Yann Btd
Yann Btd

Reputation: 419

ASP.NET Core page Razor enum dropdownlist

I use Asp.Core 2.1 with "Page Razor Crud" and I want into my page show a dropdownlist.

Here's my model:

public enum EnumStatus
    {
        Waiting,
        [Display(Name = "Development progress")]
        Progress,
        Canceled,
        Refused,
        Deployed,
    }
    public class Improvement
    {
        [Key]
        public int ID { get; set; }

        public string Title { get; set; }

        [DataType(DataType.Text)]
        public string Description { get; set; }

        public EnumStatus Status { get; set; }
    }

the CRUD Razor Page generated this line:

<select asp-for="Improvement.Status" class="form-control"></select>

But it's empty. After researchs, i found I need to add asp-items. But at this point, all solutions I tested failed.

Best regards,

Upvotes: 5

Views: 4624

Answers (1)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

Try the following code in the view:

<select asp-for="Status" 
        asp-items="Html.GetEnumSelectList<EnumStatus>()" 
        class="form-control>
    <option selected="selected" value="">Please select</option>
</select>

Here Status is the property of your Improvement model class while EnumStatus is the name of your enum type.

Upvotes: 14

Related Questions