Thursday, 1 August 2019

.Net MVC helper to generate Dropdown from Enum

.Net MVC provides helper method to generate a dropdown using Enum. Suppose you have a Roles enum and you want to generate a dropdown for roles.

//Roles Enum
public enum RolesEnum
{
    Admin = 1,
    Manager = 2,
    User = 3
}

//Model class
public class CreateUser
{
    public RolesEnum? Roles {get; set;}
}

// MVC helper
@Html.EnumDropDownListFor(m => m.Roles, "Select Role", new { @class = "form-control", data_val = true, required = "required" })

No comments:

Post a Comment