[英]Dropdown in ASP.NET MVC 3
I am new to MVC so probably confused . 我是MVC的新手,所以很困惑。 can somebody please explain me the dropdown in razor.my questions are-
有人可以向我解释一下razor中的下拉菜单吗?我的问题是-
if possible please explain with an example. 如果可能,请举例说明。
DropDownList is generated by code like this: DropDownList由以下代码生成:
@Html.DropDownList("PersonId", new SelectList(Model.People, "Id", "Text");
On the other hand, DropDownListFor is generated like this: 另一方面,DropDownListFor生成如下:
@Html.DropDownListFor(m => m.PersonId, new SelectList(Model.People, "Id", "Text")
Problem with DropDownList is that it has a magic string and if you decide to refactor the model later on, there's a high change you'll forget to change the magic string too. You could do a LINQ query like this: 您可以这样执行LINQ查询:
var datalist = New SelectList(from x in _peopleService select new SelectListItem { Text = x.Name, Value = x.Id});
If you don't have a service or an ORM between it you need to apply it to your situation, but you can generate a list like that. After nr 2, you can 在nr 2之后,您可以
datalist.Add(new SelectListItem() { Text = "Other", Value = "-1"});
Also you have to put that datalist in your viewmodel/model that is passed to the View, so you can generate a selectlist item with that. @Html.DropDownListFor(x => x.PersonId, Model.PersonList);
if you stored the list as PersonList in Model.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.