这是我的查看代码:
@using(Html.BeginForm(new { SelectedId = /*SelectedValue of DropDown*/ })) {
<fieldset>
<dl>
<dt>
@Html.Label(Model.Category)
</dt>
<dd>
@Html.DropDownListFor(model => Model.Category, CategoryList)
</dd>
</dl>
</fieldset>
<input type="submit" value="Search" />
}
如图所示代码我需要将通过dropdown
选择的值在动作BeginForm()
HTML辅助。 什么是您的建议?
因为下拉列表由表示的形式被提交时所选择的值将被传递<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);
}