ビューモデルを使用できます:
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
...
}