I have a page which creates a variable number of dropdowns and then need to set the value of the selected item but the selected value is not being set. Why is this not working? (the data is definitely correct ie Model.GroupListAll[i] does exist in the selectlist)
@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" })
}
edit: After reading stephens solution , I have done the following which really is so easy in the end:
@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] }))