I have a model which has a collection of users. I’m looping through this collection to generate a table of users. One of the options in the table is to change the user’s role, which is an enum property of the user object. Currently I have the following code:
@foreach (var user in Model.Users)
{
<tr>
...
<td>
<form action="@Url.Action("UpdateRole", "Admin", new { id = user.Id })" class="inline">
@Html.DropDownList("Role", Model.GetRoles())
</form>
</td>
...
</tr>
}
The problem with this code is that the generated drop down list is unaware of the actual value of the property on the user object. However, I see no overload for the DropDownList method that accepts a value to use for the selected value. The DropDownListFor method does, but it appears to operate directly on the model, and I can’t figure out a way to pass it the type of the nested user object. Can this be done?
You can use
SelectList[1] which has an override for the selected value, e.g.: