我有一个填充到下拉菜单中的项目集合:
string myUserName = "PopulatedWithSomeUser";
var users= from x in userRepository.GetAll()
select new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name,
Selected = (x.Name == myUserName )
};
这会正确填充对象并设置所选项目。接下来,我使用 ViewModel 将其传递给我的视图并尝试填充/选择:
我试过:
@Html.DropDownListFor(model=>model.Users,new SelectList(Model.Users),new {id = "add-user-list", name="add-user-list"})
还
@Html.DropDownListFor(model=>model.Users,new SelectList(Model.Users,"Value","Text","Selected"),new {id = "add-user-list", name="add-user-list"})
还
@Html.DropDownListFor(model=>model.Users,Model.Users, new {id = "add-user-list", name="add-user-list"})
该列表已正确填充,但从未选择我的“选定”项目。我究竟做错了什么 ?谢谢。