Search code examples
c#enumsdisplayname-attribute

Am I missing a using to use [Display(Name = "string")] on an enum?


"The type or namespace name Display cannot be found ... " "The type or namespace name Name cannot be found ... "

public enum MyEnum
{
  [Display(Name = "The First")]
  first = 1,
  [Display(Name = "The Second")]
  second,
  [Display(Name = "The Third")]
  third= 1
}

I tried [DisplayName("The First")] and that is only valid for a Class, Method Property, or Event...

What am I missing here?

UDPATE: Needed [Description()] so...

public enum MyEnum
{
  [Description("The First")]
  first = 1,
  [Description("The Second")]
  second,
  [Description("The Third")]
  third= 1
}

Solution

  • If someone ends up here because they are looking for the DisplayAttribute, it lives in the System.ComponentModel.DataAnnotations namespace. What makes things confusing is that you can add a using directive for this particular namespace, and it won't complain... but it won't find DisplayAttribute either. It stays greyed out as if the directive is unnecessary.

    Right-click on "References" in your project and select "System.ComponentModel.DataAnnotations".

    The using directive will turn black, and the Display attribute will be recognized.

    enter image description here