我的强类型 View 数据中有一个列表集合。
如何使用 Html.DropDownList 助手?
<%= Html.DropDownList(ViewData.Model.Cars) %>
(以上不起作用,似乎与任何签名都不匹配)
这是 MVC2。
最佳答案
如果您的 Car
类看起来像这样
public class Car
{
public int Id { get; set; }
public string Name { get; set; }
}
然后你像这样在你的 View 模型上放置一个属性
public int CarId { get; set; }
您生成的 View 模型如下所示
public class YourViewModel
{
public int CarId { get; set; }
public List<Car> Cars { get; set; }
}
你可以这样做
this.Html.DropDownListFor(x => x.CarId, new SelectList(this.Model.Cars, "Id", "Name"))
发布时,如果 View 模型是 Action
的参数,CarId
将被绑定(bind)到默认模型绑定(bind)器的边界。
关于c# - 使用 List<Cars> 的下拉列表的 MVC 助手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9218228/