asp.net-mvcdisplayattribute

what is the advantage of DisplayName attribute than Display attribute?


The result of [DisplayName("foo")] and [Display(Name="foo")] for @Html.LabelFor(x => x.MyProperty) is same. both generate <label for="MyProperty">foo</label>. what is the advantage of DisplayName?


Solution

  • Display attribute should be preferred over DisplayName attribute. The former one comes form DataAnnotations namespace and exposes more properties than the later one, ie. ShortName, Prompt, GroupName, Order, Description, which are used by DataAnnotationsMetadataProvider.

    Most importantly, DisplayName accepts only literals, while Display accepts resource type and key, which can be used to provide translations using separate resource files (resx).