使用Html.DropDownListFor得到一个价值选择 [英] Use Html.DropDownListFor to get a selected value

查看:141
本文介绍了使用Html.DropDownListFor得到一个价值选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Html.DropDownListFor建立一个下拉列表,让用户选择的值。我可以列表显示,但无法弄清楚如何去传递选定值的链接。结果
我有以下型号:

I am trying to use Html.DropDownListFor to build a dropdown list and get the user selected value. I can get the list to display but cannot figure out how to get the link to pass the selected value.
I have the following model:

public partial class ProductVariant
{
    public int ID { get; set; }
    public string Sku { get; set; }
}

以下视图模型:

public class SkuToDiscountViewModel
{
    public int ID { get; set; }
    public string Sku { get; set; }
    public IEnumerable<ProductVariant> Products { get; set; }
}

以下控制器动作:

The following Controller action:

public ViewResult Index()
    {
       SkuToDiscountViewModel sModel = new SkuToDiscountViewModel();
        List<ProductVariant> prodSkus = db.ProductVariants.ToList();
        sModel.Products = prodSkus;
        return View(sModel);
    }

以下观点:

@model mySpace.ViewModel.SkuToDiscountViewModel
@{
    ViewBag.Title = "Index";
 }
 <h2>Index</h2>
 @using(Html.BeginForm())
    {
       Html.DropDownListFor(x=>x.ID,
       new SelectList(Model.Products,"ID", "Sku", Model.ID), " select ")
      <p>
          @Html.ActionLink("Edit", "Edit") 
      </p>
    }

任何帮助是AP preciated。

Any help is appreciated.

推荐答案

您需要一个提交按钮到您的窗体:

You need to a submit button to your form:

@model mySpace.ViewModel.SkuToDiscountViewModel
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@using(Html.BeginForm())
{
    Html.DropDownListFor(x=>x.ID,
        new SelectList(Model.Products,"ID", "Sku", Model.ID), " select ")
    <p>
        <input type="submit" value="Save" />
    </p>
}

然后,你需要一个Action添加到您的控制器是这样的:

Then you need to add an Action to your controller like this:

[HttpPost]
public ActionResult Index(SkuToDiscountViewModel postedModel)
{
    // postedModel.ID will contain the value selected in the drop down list
}

这篇关于使用Html.DropDownListFor得到一个价值选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆