I’m confused as to why the corresponding option in the select list is not being selected.
When I step through the loop, item.DisciplineId = 2, but School is not selected (“Choose” still is). Any suggestions ?
View
<% foreach (var item in Model.TeamMembers)
{ %>
<tr>
<td>
<%: Html.DropDownListFor(m => item.DisciplineId, Model.MemberDisciplines, "Choose") %>
</td>
</tr>
<% } %>
View Model
public SelectList MemberDisciplines { get; set; }
public IEnumerable<TeamMember> TeamMembers { get; set; }//Set from the model
MemberDisciplines = new SelectList(new[] {
new SelectListItem{ Text = "Technical", Value = "1"},
new SelectListItem{ Text = "School", Value = "2"},
new SelectListItem{ Text = "Health", Value = "3"}
}, "Value", "Text");
Alright, I would then suggest you to convert MemberDisciplines property into a method with the following content:
And then you call it in your DropDownListFor like so: