Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • andrew shearer 506 posts 653 karma points
    Jun 10, 2015 @ 00:12
    andrew shearer
    1

    possible to use Description instead?

    hello - if my enum is annotated with a description attribute would it be possible for the datatype to use this for the label in the backoffice?

    [Description("my description")]

    thanks

     

  • Bartosz Ladziński 20 posts 154 karma points
    Oct 28, 2015 @ 10:27
    Bartosz Ladziński
    0

    Hey, Do you still need to achieve that? I had that problem yesterday and I managed to fix it with the following changes in the EnumApiController in Enum Lists project.

    static EnumApiController()
            {
                Assembly.ReflectionOnlyLoad("System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089");
            }
    
            public IDictionary<string, string> GetAll(string assemblyName, string typeName)
            {            
                var assembly = GetAssembly(assemblyName);
                var type = assembly.GetType(typeName);            
                Dictionary<string, string> items = new Dictionary<string,string>();
                foreach (FieldInfo fi in type.GetFields(BindingFlags.Public | BindingFlags.Static))            
                {
                    items.Add(fi.Name, GetEnumDescription(fi));                
                }
    
                return items;
            }
    
    public static string GetEnumDescription(FieldInfo fi)
            {                 
                List<CustomAttributeData> attributes =
                    fi.GetCustomAttributesData().ToList();
    
    
                if (attributes != null &&
                    attributes.Count > 0)
                {
                    var attribute = attributes.Where(x => x.AttributeType.Name == "DescriptionAttribute").FirstOrDefault();
                    if (attribute != null)
                        return attribute.ConstructorArguments[0].Value.ToString();
                    else
                        return fi.Name;
                }                
                else
                    return fi.Name;
            }
    

    Then you just need to change the js files to be able handle the Dictionary that's returned. For checkbox it would be something like that:

    for (var i in $scope.enumItems) {
                        var isChecked = _.contains($scope.model.value, i);
                        $scope.selectedItems.push({ checked: isChecked, key: i, val: $scope.enumItems[i] });
                    }
    
Please Sign in or register to post replies

Write your reply to:

Draft