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

后端 未结 20 2417
孤街浪徒
孤街浪徒 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:42

    I tried doing this as an edit but it was rejected; I can't see why.

    The above will throw an exception if you call it with an Enum that has a mix of custom attributes and plain items, e.g.

    public enum CommentType
    {
        All = 1,
        Rent = 2,
        Insurance = 3,
        [Display(Name="Service Charge")]
        ServiceCharge = 4
    }
    

    So I've modified the code ever so slightly to check for custom attributes before trying to access them, and use the name if none are found.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.Reflection;
    
    public static class EnumHelper
    {
        public static IList GetValues(Enum value)
        {
            var enumValues = new List();
    
            foreach (FieldInfo fi in value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                enumValues.Add((T)Enum.Parse(value.GetType(), fi.Name, false));
            }
            return enumValues;
        }
    
        public static T Parse(string value)
        {
            return (T)Enum.Parse(typeof(T), value, true);
        }
    
        public static IList GetNames(Enum value)
        {
            return value.GetType().GetFields(BindingFlags.Static | BindingFlags.Public).Select(fi => fi.Name).ToList();
        }
    
        public static IList GetDisplayValues(Enum value)
        {
            return GetNames(value).Select(obj => GetDisplayValue(Parse(obj))).ToList();
        }
    
        private static string lookupResource(Type resourceManagerProvider, string resourceKey)
        {
            foreach (PropertyInfo staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public))
            {
                if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager))
                {
                    System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null, null);
                    return resourceManager.GetString(resourceKey);
                }
            }
    
            return resourceKey; // Fallback with the key name
        }
    
        public static string GetDisplayValue(T value)
        {
            var fieldInfo = value.GetType().GetField(value.ToString());
    
            var descriptionAttributes = fieldInfo.GetCustomAttributes(
                typeof(DisplayAttribute), false) as DisplayAttribute[];
    
            if (descriptionAttributes.Any() && descriptionAttributes[0].ResourceType != null)
                return lookupResource(descriptionAttributes[0].ResourceType, descriptionAttributes[0].Name);
    
            if (descriptionAttributes == null) return string.Empty;
            return (descriptionAttributes.Length > 0) ? descriptionAttributes[0].Name : value.ToString();
        }
    }
    

提交回复
热议问题