Reputation: 733
I want to populate the select control with the values of the CivilStatus
enum:
I have these models:
public class MissionPartner
{
[Key]
public int ParnterId { get; set; }
public CivilStatus MaritalStatus{ get; set; }
}
public enum CivilStatus
{
Single,
Married
}
And I am using this code in the view:
<div class="form-group">
<label asp-for="MaritalStatus" class="control-label"></label>
<select asp-for="MaritalStatus" class="form-control"></select>
<span asp-validation-for="MaritalStatus" class="text-danger"></span>
</div>
Generated view:
As you can see, there are no values in the control.
Upvotes: 0
Views: 487
Reputation: 390
It looks like this is scheduled to be fixed in .NET 8: https://github.com/dotnet/Scaffolding/issues/1885
Upvotes: 0
Reputation: 9815
In your template do the following (and adjust to your needs):
<select asp-for="MaritalStatus" asp-items="Html.GetEnumSelectList<CivilStatus>()">
<option selected="selected" value="">Please select</option>
</select>
Upvotes: 1