0

我正在尝试使用 Html.DropDownListFor 来构建下拉列表并获取用户选择的值。我可以显示列表,但无法弄清楚如何让链接传递所选值。
我有以下模型:

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; }
}

以下控制器操作:

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>
    }

任何帮助表示赞赏。

4

1 回答 1

2

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>
}

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
}
于 2013-01-28T18:44:20.560 回答