Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can retrieve the Display Name Attribute of an Enum member through MVC Razor coding by using the following code:

@using System.ComponentModel.DataAnnotations;
@{
    //Your Enum
    enum YourEnum
    {
        [Display(Name = "First Option")]
        Option1,
        [Display(Name = "Second Option")]
        Option2
    }
    //Get the Display Name of enum member
    var displayName = typeof(YourEnum).GetMember(Model.Status.ToString()).First().GetCustomAttribute<DisplayAttribute>().Name;
}

In the above code, you need to replace "YourEnum" with your actual Enum name and "Model" with your actual ViewModel name.

After executing the above code, "displayName" will contain the Display Name of the Enum member based on the value of "Model.Status".