What is difference between DisplayName
attribute and Display
attribute in ASP.NET MVC?
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.
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.
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.
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 theSystem.ComponentModel.DataAnnotations.dll
assembly - can be used on parameters and fields
- lets you set additional properties like
Description
orShortName
- can be localized with resources
DisplayNameAttribute
- DisplayName is in the
System.ComponentModel
namespace inSystem.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.
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)
© 2022 - 2024 — McMap. All rights reserved.