ドロップダウン リストは要素によって表されるため、フォームが送信されると、選択された値が渡されます<select>
。SelectedId
たとえば、ドロップダウンをバインドするプロパティが呼び出されるように、ビューモデルを調整する必要があります。
@using(Html.BeginForm() )
{
<fieldset>
<dl>
<dt>
@Html.LabelFor(x => x.SelectedId)
</dt>
<dd>
@Html.DropDownListFor(x => x.SelectedId, Model.CategoryList)
</dd>
</dl>
</fieldset>
<input type="submit" value="Search" />
}
これは、次のビュー モデルを前提としています。
public class MyViewModel
{
[DisplayName("Select a category")]
public int SelectedId { get; set; }
public IEnumerable<SelectListItem> CategoryList { get; set; }
}
これはコントローラーによって処理されます。
public ActionResult Index()
{
var model = new MyViewModel();
// TODO: this list probably comes from a repository or something
model.CategoryList = new[]
{
new SelectListItem { Value = "1", Text = "category 1" },
new SelectListItem { Value = "2", Text = "category 2" },
new SelectListItem { Value = "3", Text = "category 3" },
};
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// here you will get the selected category id in model.SelectedId
return Content("Thanks for selecting category id: " + model.SelectedId);
}