John
John

Reputation: 733

Scaffolding of Enum as a Select Control but no values are seen

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:

enter image description here

As you can see, there are no values in the control.

Upvotes: 0

Views: 487

Answers (2)

Robin Wilson
Robin Wilson

Reputation: 390

It looks like this is scheduled to be fixed in .NET 8: https://github.com/dotnet/Scaffolding/issues/1885

Upvotes: 0

alsami
alsami

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

Related Questions