I have a collection of items populated to a drop down menu:
string myUserName = "PopulatedWithSomeUser";
var users= from x in userRepository.GetAll()
select new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name,
Selected = (x.Name == myUserName )
};
This correctly populates an object and sets the selected item. Next I pass this to my View with a ViewModel and try to Populate/select:
I tried:
@Html.DropDownListFor(model=>model.Users,new SelectList(Model.Users),new {id = "add-user-list", name="add-user-list"})
also
@Html.DropDownListFor(model=>model.Users,new SelectList(Model.Users,"Value","Text","Selected"),new {id = "add-user-list", name="add-user-list"})
also
@Html.DropDownListFor(model=>model.Users,Model.Users, new {id = "add-user-list", name="add-user-list"})
The list is populated correctly but my "selected" item is never selected. What am I doing wrong ? Thanks.