我有一个页面,它创建了可变数量的下拉菜单,然后需要设置所选项目的值,但未设置所选值。为什么这不起作用? (数据肯定是正确的,即Model.GroupListAll [i]确实存在于选择列表中)
@for (int i = 0; i < Model.GroupListAll.Count(); i++)
{
@Html.DropDownList("GroupListAll", Model.GroupList.Select(x => new SelectListItem(){Text = x.Text, Value= x.Value, Selected = x.Value== Model.GroupListAll[i] } ), new { @class = "form-control group-list all-list", style = "width:187px" })
}
编辑:在阅读了斯蒂芬斯解决方案之后,我已经完成了以下操作,最终真的很容易:
@for (int i = 0; i < Model.GroupListAny.Count(); i++)
{
@Html.DropDownListFor(x => x.GroupListAny[i], Model.GroupList.Select(x => new SelectListItem() { Text = x.Text, Value = x.Value, Selected = x.Value == Model.GroupListAny[i] }))