在我看来
<%= Html.DropDownListFor( x => x.Countries[ i ], Model.CountryList )%>
在我的 Controller 中
public int[ ] Countries { get; set; }
public List<SelectListItem> CountryList { get; set; }
当表单发布时,没有问题,下拉列表将被填充,并且用户选择的值将被发布。但是,当我尝试加载已为“国家/地区”[] 指定值的表单时,它不会被选中。
最佳答案
不确定这是否是 mv4 的新功能或者是否存在于之前的版本中。但是 DropDownListFor 包含一个用于 SelectList 构造函数的附加参数。
SelectList(IEnumerable, String, String, Object)
例如:
Html.DropDownListFor( x => x.Countries[ i ], New SelectList(Model.CountryList,"ID","Description",Model.Countries[i]))
其中 ID
是 CountryList
对象中的国家/地区 ID,Description
是国家/地区名称。
关于asp.net-mvc - 如果在 for 循环中,DropDownListFor 不会选择值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2631397/