cant get Enum Display Name property in razor view
10,329
like this:
public static class EnumExtensions
{
public static string GetDisplayName(this Enum enumValue)
{
return enumValue.GetType()
.GetMember(enumValue.ToString())
.First()
.GetCustomAttribute<DisplayAttribute>()
.GetName();
}
}
& the view :
@Model.LectureGig.Gender.GetDisplayName()
Author by
Assaf Our
Updated on June 05, 2022Comments
-
Assaf Our almost 2 years
I have an enum with Display(Name=) that I wish to display in a Razor view but I'm getting only the value.
public enum Gender { [Display(Name = "Man woman")] EveryOne = 0, [Display(Name = "Man")] Man = 1, [Display(Name = "Woman")] Woman = 2 }
Razor:
Is for: @Model.LectureGig.Gender
The Html results are:
Is for: Everyone
Instead of:
Is for: Man woman
-
kite over 2 yearsthis works; Just need to handle enum that has no Display attribute