11

I have this Enum (Notebook.cs):

public enum Notebook : byte
{
   [Display(Name = "Notebook HP")]
   NotebookHP,

   [Display(Name = "Notebook Dell")]
   NotebookDell
}

Also this property in my class (TIDepartment.cs):

public Notebook Notebook { get; set; }

It's working perfectly, I just have one "problem":

I created an EnumDDLFor and it's showing the name I setted in DisplayAttribute, with spaces, but the object doesn't receive that name in DisplayAttribute, receives the Enum name (what is correct), so my question is:

Is there a way to receive the name with spaces which one I configured in DisplayAttribute?

3
  • How are you using the enums for the Notebook property? Your code is bit confusing.
    – Yosep Kim
    May 26, 2015 at 19:56
  • I edited, can you look now? May 26, 2015 at 20:08
  • It seems that ASP.Net Core 3.0 now supports dataannotations on Enum values as you have typed it above.
    – Steve
    Aug 21, 2020 at 15:01

3 Answers 3

11

MVC doesn't make use of the Display attribute on enums (or any framework I'm aware of). You need to create a custom Enum extension class:

public static class EnumExtensions
{
    public static string GetDisplayAttributeFrom(this Enum enumValue, Type enumType)
    {
        string displayName = "";
        MemberInfo info = enumType.GetMember(enumValue.ToString()).First();

        if (info != null && info.CustomAttributes.Any())
        {
            DisplayAttribute nameAttr = info.GetCustomAttribute<DisplayAttribute>();
            displayName = nameAttr != null ? nameAttr.Name : enumValue.ToString();
        }
        else
        {
            displayName = enumValue.ToString();
        }
        return displayName;
    }
}

Then you can use it like this:

Notebook n = Notebook.NotebookHP;
String displayName = n.GetDisplayAttributeFrom(typeof(Notebook));

EDIT: Support for localization

This may not be the most efficient way, but SHOULD work.

public static class EnumExtensions
{
    public static string GetDisplayAttributeFrom(this Enum enumValue, Type enumType)
    {
        string displayName = "";
        MemberInfo info = enumType.GetMember(enumValue.ToString()).First();

        if (info != null && info.CustomAttributes.Any())
        {
            DisplayAttribute nameAttr = info.GetCustomAttribute<DisplayAttribute>();

            if(nameAttr != null) 
            {
                // Check for localization
                if(nameAttr.ResourceType != null && nameAttr.Name != null)
                {
                    // I recommend not newing this up every time for performance
                    // but rather use a global instance or pass one in
                    var manager = new ResourceManager(nameAttr.ResourceType);
                    displayName = manager.GetString(nameAttr.Name)
                }
                else if (nameAttr.Name != null)
                {
                    displayName = nameAttr != null ? nameAttr.Name : enumValue.ToString();
                }
            }
        }
        else
        {
            displayName = enumValue.ToString();
        }
        return displayName;
    }
}

On the enum, the key and resource type must be specified:

[Display(Name = "MyResourceKey", ResourceType = typeof(MyResourceFile)]
8
  • Works like a charm! :) Thanks. May 26, 2015 at 20:29
  • 1
    It doesn't work :( // "ResourceType is a 'type' but is used like a 'variable'. And what is ResFileAssembly? May 27, 2015 at 20:54
  • Updated my answer, added the new keyword for the resource manager and am using the resource type specified by the ResourceType in the enum's annotation. I tested this locally and it works.
    – akousmata
    May 27, 2015 at 21:51
  • It's not compiling.. can't instantiate the ResourceManager, and getString method also not works. May 28, 2015 at 2:20
  • 1
    @MarkGood thanks for the heads up. I updated the answer.
    – akousmata
    Dec 7, 2015 at 16:03
8

Here's a simplified (and working) version of akousmata's localized enum extension:

public static string DisplayName(this Enum enumValue)
{
    var enumType = enumValue.GetType();
    var memberInfo = enumType.GetMember(enumValue.ToString()).First();

    if (memberInfo == null || !memberInfo.CustomAttributes.Any()) return enumValue.ToString();

    var displayAttribute = memberInfo.GetCustomAttribute<DisplayAttribute>();

    if (displayAttribute == null) return enumValue.ToString();

    if (displayAttribute.ResourceType != null && displayAttribute.Name != null)
    {
        var manager = new ResourceManager(displayAttribute.ResourceType);
        return manager.GetString(displayAttribute.Name);
    }

    return displayAttribute.Name ?? enumValue.ToString();
}

Note: I move enumType from a parameter to a local variable.

Example usage:

public enum IndexGroupBy 
{
    [Display(Name = "By Alpha")]
    ByAlpha,
    [Display(Name = "By Type")]
    ByType
}

And

@IndexGroupBy.ByAlpha.DisplayName()

Here is a editor template that can be used with the extension method above:

@model Enum

@{    
    var listItems = Enum.GetValues(Model.GetType()).OfType<Enum>().Select(e =>
        new SelectListItem
        {
            Text = e.DisplayName(),
            Value = e.ToString(),
            Selected = e.Equals(Model)
        });
    var prefix = ViewData.TemplateInfo.HtmlFieldPrefix;
    var index = 0;
    ViewData.TemplateInfo.HtmlFieldPrefix = string.Empty;

    foreach (var li in listItems)
    {
        var fieldName = string.Format(CultureInfo.InvariantCulture, "{0}_{1}", prefix, index++);
        <div class="editor-radio">
            @Html.RadioButton(prefix, li.Value, li.Selected, new {@id = fieldName})
            @Html.Label(fieldName, li.Text)
        </div>
    }
    ViewData.TemplateInfo.HtmlFieldPrefix = prefix;
}

And here is an example usage:

@Html.EditorFor(m => m.YourEnumMember, "Enum_RadioButtonList")
1
  • 2
    Good catch on getting the type directly from the enum. Much cleaner approach.
    – akousmata
    Dec 7, 2015 at 16:09
1

Since you are worrying about visuals I would use a configurable approach:

public NotebookTypes NotebookType;

public enum NotebookTypes{
   NotebookHP,
   NotebookDell
}

public string NotebookTypeName{
   get{
      switch(NotebookType){
         case NotebookTypes.NotebookHP:
            return "Notebook HP"; //You may read the language dependent value from xml...
         case NotebookTypes.NotebookDell:
            return "Notebook Dell"; //You may read the language dependent value from xml...
         default:
            throw new NotImplementedException("'" + typeof(NotebookTypes).Name + "." + NotebookType.ToString() + "' is not implemented correctly.");
      }
   }
}
9
  • I understood your answer, but is the only way? (If I have 15 enums, I have to do it for all).. Btw, I'm worring about visual because I have some enums like it: 'HigherEducationIncomplete', I think it looks horrible, I just want show 'Higher Education Incomplete' (as I configured in DisplayAttribute), can you understand now? May 26, 2015 at 20:12
  • @developer033 No certainly not. But this approach let's you configure the visuals for multilpe languages. May 26, 2015 at 21:04
  • It's working with the @akousmata 's answer. Anyway, thanks, +1 for try. May 26, 2015 at 21:09
  • 1
    @NoelWidmer you can still support localization through resource files and the provided solution doesn't need to change. That wasn't what the OP was asking but he could simply change his enum to [Display(Name = ResFileName.ResFileValue)] and the answer would still work.
    – akousmata
    May 27, 2015 at 14:51
  • 1
    I have a working version that supports localization through resource files in my updated answer if you want to check it out.
    – akousmata
    May 27, 2015 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.