在asp.net MVC中获取dropdownlist的选定值
问题描述:
如何获取下拉列表的选择值,这里是我的代码正常工作。
how can i get select value of dropdownlist and here is my code which is working fine.
var list = new[] {
new Person { Id = 1, Name = "Name1" },
new Person { Id = 2, Name = "Name2" },
new Person { Id = 3, Name = "Name3" }
};
var selectList = new SelectList(list, "Id", "Name", 2);
ViewData["People"] = selectList;
<%= Html.DropDownListFor(model => model.Peoples, ViewData["People"] as IEnumerable<SelectListItem>)%>
答
model.Peoples
属性,您在 DropDownListFor
中使用的属性必须为整数。它将被绑定到您提交此表单的控制器操作中的选定值:
The model.Peoples
property which you are using in the DropDownListFor
must be of type integer. It will be bound to the selected value in the controller action to which you are submitting this form:
[HttpPost]
public ActionResult Index(YourModelType model)
{
int selectedPersonId = model.Peoples;
// TODO: do something with the id
return View();
}
备注:人民币更符合语法正确的名称
属性将为 PersonId
。