displayname attribute vs display attribute
Asked Answered
E

4

156

What is difference between DisplayName attribute and Display attribute in ASP.NET MVC?

Explain answered 9/3, 2011 at 9:25 Comment(0)
P
78

They both give you the same results but the key difference I see is that you cannot specify a ResourceType in DisplayName attribute. For an example in MVC 2, you had to subclass the DisplayName attribute to provide resource via localization. Display attribute (new in MVC3 and .NET4) supports ResourceType overload as an "out of the box" property.

Perspire answered 24/5, 2011 at 12:27 Comment(1)
Good to know! Was about to implement a custom attribute to load the display value from the .resx file, then happened on this question. Display attribute is the way to go when developing localized applications.Misteach
R
166

DisplayName sets the DisplayName in the model metadata. For example:

[DisplayName("foo")]
public string MyProperty { get; set; }

and if you use in your view the following:

@Html.LabelFor(x => x.MyProperty)

it would generate:

<label for="MyProperty">foo</label>

Display does the same, but also allows you to set other metadata properties such as Name, Description, ...

Brad Wilson has a nice blog post covering those attributes.

Raphael answered 9/3, 2011 at 9:30 Comment(2)
+1 - probably a much more helpful than mine (now deleted); too early in the morning for me :)Retool
I had problems with PropertyDescriptor and using Display(name.. when calling @descriptor.DisplayName, it will only work using DisplayName, not Display(name..).Lenna
P
78

They both give you the same results but the key difference I see is that you cannot specify a ResourceType in DisplayName attribute. For an example in MVC 2, you had to subclass the DisplayName attribute to provide resource via localization. Display attribute (new in MVC3 and .NET4) supports ResourceType overload as an "out of the box" property.

Perspire answered 24/5, 2011 at 12:27 Comment(1)
Good to know! Was about to implement a custom attribute to load the display value from the .resx file, then happened on this question. Display attribute is the way to go when developing localized applications.Misteach
I
43

I think the current answers are neglecting to highlight the actual important and significant differences and what that means for the intended usage. While they might both work in certain situations because the implementer built in support for both, they have different usage scenarios. Both can annotate properties and methods but here are some important differences:

DisplayAttribute

  • defined in the System.ComponentModel.DataAnnotations namespace in the System.ComponentModel.DataAnnotations.dll assembly
  • can be used on parameters and fields
  • lets you set additional properties like Description or ShortName
  • can be localized with resources

DisplayNameAttribute

  • DisplayName is in the System.ComponentModel namespace in System.dll
  • can be used on classes and events
  • cannot be localized with resources

The assembly and namespace speaks to the intended usage and localization support is the big kicker. DisplayNameAttribute has been around since .NET 2 and seems to have been intended more for naming of developer components and properties in the legacy property grid, not so much for things visible to end users that may need localization and such.

DisplayAttribute was introduced later in .NET 4 and seems to be designed specifically for labeling members of data classes that will be end-user visible, so it is more suitable for DTOs, entities, and other things of that sort. I find it rather unfortunate that they limited it so it can't be used on classes though.

EDIT: Looks like latest .NET Core source allows DisplayAttribute to be used on classes now as well.

Innocency answered 12/8, 2016 at 14:1 Comment(0)
W
16

Perhaps this is specific to .net core, I found DisplayName would not work but Display(Name=...) does. This may save someone else the troubleshooting involved :)

//using statements
using System;
using System.ComponentModel.DataAnnotations;  //needed for Display annotation
using System.ComponentModel;  //needed for DisplayName annotation

public class Whatever
{
    //Property
    [Display(Name ="Release Date")]
    public DateTime ReleaseDate { get; set; }
}


//cshtml file
@Html.DisplayNameFor(model => model.ReleaseDate)
Wreckfish answered 8/12, 2016 at 1:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.