I tried this accepted answer, but it seems like this may be handled differently in the latest version of ASP.NET Core. I got no selected
attribute at all from this:
<select class="form-control" asp-for="Status">
<option></option>
@foreach (var option in Enum.GetNames(typeof(StatusOptions))) {
<option value="@option" @{if (option == Model.Status) { <text>selected="true"</text> } }>@option</option>
}
</select>
As an alternative, I tried this and got selected="selected"
, but Visual Studio gives me a warning that @selected is not a valid value of attribute of 'selected'
.
<select class="form-control" asp-for="Status">
<option></option>
@foreach (var option in Enum.GetNames(typeof(StatusOptions))) {
bool selected = option == Model.Status;
<option value="@option" selected="@selected">@option</option>
}
</select>
I can use that, but I have to ignore the warning and it doesn't render exactly what I want. Is there a way to get:
<option value="firstOption" selected>firstOption</option>