DropDownList的孔型的SelectedValue在Html.BeginForm()在ASP.NEt MVC 3 [英] Pass SelectedValue of DropDownList in Html.BeginForm() in ASP.NEt MVC 3
本文介绍了DropDownList的孔型的SelectedValue在Html.BeginForm()在ASP.NEt MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的看法code:
This is my View Code:
@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" />
}
如图所示code,我需要在下拉菜单
选择的值传递给在 BeginForm)的动作(
HTML辅助。你的建议是什么?
As code shown I need to pass the dropdown
selected value to the action in BeginForm()
Html helper. What is your suggestion?
推荐答案
在表单提交,因为下拉列表是重新通过&LT psented $ P $所选择的值将被传递;选择&GT;
元素。你只需要调整您的视图模型,以便它有一个叫做物业 SelectedId
例如要在其中绑定下拉:
The selected value will be passed when the form is submitted because the dropdown list is represented by a <select>
element. You just need to adapt your view model so that it has a property called SelectedId
for example to which you will bind the dropdown:
@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; }
}
这将您的控制器来处理:
that will be handled by your controller:
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);
}
这篇关于DropDownList的孔型的SelectedValue在Html.BeginForm()在ASP.NEt MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文