如何在Action中选择drow down list值?

在MVC Web应用程序中,它是一个具有强类型模型的视图,其中按模型生成/绑定下拉列表。

以下是查看代码:

@model LoanViewModel 

@Html.ValidationSummary()

Select an Item : @Html.DropDownListFor(x => x.HomeLoanLead.Items, new SelectList(Model.HomeLoanLead.Items), "--Choose any Item--")

在模型中我有下载列表的硬编码选项:

  public List Items { get { _items = new List(); _items.Add("One"); _items.Add("Two"); _items.Add("Three"); return _items; } } 

在回帖后我无法获得下拉列表中的选定值。 请指导我如何进入后期操作选择了下拉值。

使用Html.DropDownFor()显示选项列表并绑定到属性的简单示例:

模型

 public class LoanViewModel { [Required] [Display(Name="Select Item")] public string Item { get; set; } public SelectList ItemList { get; set; } } 

调节器

 public ActionResult Edit() { LoanViewModel model = new LoanViewModel(); model.Item = "Two"; // this will now pre-select the second option in the view ConfigureEditModel(model); return View(model); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(LoanViewModel model) { if (!ModelState.IsValid) { ConfigureEditModel(model); // repopulate select list return View(model); // return the view to correct errors } // If you want to validate the the value is indeed one of the items ConfigureEditModel(model); if (!model.ItemList.Contains(model.Item)) { ModelState.AddModelError(string.Empty, "I'm secure!"); return View(model); } string selectedItem = model.Item; .... // save and redirect } private void ConfigureEditModel(LoanViewModel model) { List items = new List() { "One", "Two", "Three" }; model.ItemList = new SelectList(items); // create the options // any other common stuff } 

视图

 @model LoanViewModel @using (Html.BeginForm()) { @Html.AntiForgeryToken() @Html.ValidationSummary(true) @Html.DisplayFor(m => m.Item) @Html.DropDownListFor(m => m.Item, Model.ItemList), "--Choose any Item--") @Html.ValidationMessageFor(m => m.Item)  }