How to get the Display Name Attribute of an Enum member via MVC razor code?

后端 未结 20 2394
孤街浪徒
孤街浪徒 2020-11-22 09:30

I\'ve got a property in my model called \"Promotion\" that its type is a flag enum called \"UserPromotion\". Members of my enum have display attributes set as follows:

20条回答
  •  盖世英雄少女心
    2020-11-22 09:55

    With Core 2.1,

    public static string GetDisplayName(Enum enumValue)
    {
      return enumValue.GetType()?
     .GetMember(enumValue.ToString())?[0]?
     .GetCustomAttribute()?
     .Name;
    }
    

提交回复
热议问题