您可以使用视图模型:
public class MyViewModel
{
public string Value { get; set; }
public IEnumerable<SelectListItem> { get; set; }
}
那么你可以有一个控制器来填充这个模型并将它传递给视图:
public ActionResult Index()
{
var model = new MyViewModel();
// TODO: you could load the values from your database here
model.Values = new[]
{
new SelectListItem { Value = "Objective", Text = "Objective" },
new SelectListItem { Value = "Subjective", Text = "Subjective" },
};
return View(model);
}
然后有一个相应的强类型视图,您将在其中使用Html.DropDownListFor
帮助器:
@model MyViewModel
@using (Html.BeginForm())
{
@Html.DropDownListFor(x => x.Value, Model.Values, "Select any value");
<button type="submit">OK</button>
}
最后,您可以有一个相应的控制器操作,表单将被提交到该控制器操作,并将视图模型作为参数:
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// model.Value will contain the selected value here
...
}