您应该为下拉列表命名(不需要 id):
<select id="drpType" name="drpType">
<option value="0">Single</option>
<option value="1">Married</option>
</select>
然后在提交表单的相应控制器操作中,您可以有一个同名的参数,该参数将包含所选值:
[HttpPost]
public ActionResult SomeAction(string drpType)
{
// ... drpType will contain the selected value (0 or 1)
}
但是实现这一点的正确方法是使用视图模型:
public class MyViewModel
{
[Display(Name = "Marital status")]
public string SelectedMaritalStatus { get; set; }
public IEnumerable<SelectListItem> MaritalStatuses
{
get
{
return new[]
{
new SelectListItem { Value = "0", Text = "Single" },
new SelectListItem { Value = "1", Text = "Married" },
};
}
}
}
然后您可以拥有一个具有 2 个操作的控制器(一个用于呈现表单,一个用于处理结果):
public class HomeController: Controller
{
public ActionResult Index()
{
var model = new MyViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// ... model.SelectedMaritalStatus will contain the selected value (0 or 1)
}
}
现在您可以拥有一个强类型视图,您可以在其中使用 DropDownListFor 助手:
@model MyViewModel
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(x => x.SelectedMaritalStatus)
@Html.DropDownListFor(x => x.SelectedMaritalStatus, Model.MaritalStatuses)
</div>
<button type="submit">OK</button>
}