2

I have a property in my class that I can set to an enum value, the enum has the flag attribute. I want to display the Display name of the enum in my view, it works when I've set the Person.Types to a single value but not when it's set to multiple values.

[Flags]
public enum TypesEnum
{
    [Display(Name = "Lärare")]
    Teacher = 1,
    [Display(Name = "Student")]
    Student = 2
}

public class Person
{
    public string Name { get; set; }
    public TypesEnum Types { get; set; }
}

person.Types = TypesEnum.Teacher | TypesEnum.Student;

var model = db.Persons
            .Where(x => x.Types.HasFlag(TypesEnum.Teacher))
            .ToList();

I've used this helper method to get the DisplayNameAttribute when the person only have one type. But when the person have two (eg teacher and student) I get an InvalidOperationException: Sequence contains no elements on enumValue.GetType()

public static string GetDisplayName(this Enum enumValue)
{
    return enumValue.GetType()
                    .GetMember(enumValue.ToString())
                    .First()
                    .GetCustomAttribute<DisplayAttribute>()
                    .GetName();
}

Then in razor view:

@foreach (var person in Model)
{
    <h3>@person.Name<h3>
    <p>@person.Types</p>
}

What I expect as an output from person.Types.GetDisplayName() is "Lärare, Student" instead of "Teacher, Student" as I get from just person.Types. I'm using .NET Core.

Rob
  • 186
  • 5
  • 18
  • http://stackoverflow.com/questions/13099834/how-to-get-the-display-name-attribute-of-an-enum-member-via-mvc-razor-code – Ashiquzzaman May 14 '17 at 13:43
  • @Ashiquzzaman doesn't that also only give the displayname when you have only one value? If you check the comments and my post I'm already using an extension people suggest on that topic. – Rob May 15 '17 at 07:07

1 Answers1

4

Here is one way to do it with some static helpers:

    /// <summary>
    /// Gets the display name for an enum.
    /// </summary>
    /// <param name="enumValue"></param>
    /// <exception cref="ArgumentException"></exception>
    /// <returns></returns>
    public static string GetDisplayName(this Enum enumValue)
    {
        var enumType = enumValue.GetType();
        var names = new List<string>();
        foreach (var e in Enum.GetValues(enumType))
        {
            var flag = (Enum)e;              
            if(enumValue.HasFlag(flag))
            {
                names.Add(GetSingleDisplayName(flag));
            }
        }
        if (names.Count <= 0) throw new ArgumentException();
        if (names.Count == 1) return names.First();
        return string.Join(",", names);
    }

    /// <summary>
    /// Gets the display value for a single enum flag or 
    /// name of that flag if the display value is not set
    /// </summary>
    /// <param name="flag"></param>
    /// <returns></returns>
    public static string GetSingleDisplayName(this Enum flag)
    {
        try
        {
            return flag.GetType()
                    .GetMember(flag.ToString())
                    .First()
                    .GetCustomAttribute<DisplayAttribute>()
                    .Name;
        }
        catch
        {
            return flag.ToString();
        }
    }
rahicks
  • 573
  • 7
  • 19