Tuesday, March 27, 2018

How to create a dropdown-list from an Enum in ASP.NET MVC (5.1 & above)


In ASP.NET MVC 5.1, you can use EnumDropDownListFor() helper, so no need for custom extensions:


Model:
public enum MyEnum
{
    [Display(Name = "First Value - desc..")]
    FirstValue,
    [Display(Name = "Second Value - desc...")]
    SecondValue
}
View:
@Html.EnumDropDownListFor(model => model.MyEnum)
Using Tag Helper (ASP.NET MVC 6):
<select asp-for="@Model.SelectedValue" asp-items="Html.GetEnumSelectList<MyEnum>()">

No comments:
Write comments
Recommended Posts × +