Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 815
Ghooti Farangi Avatar asked Mar 09 '11 19:03

Ghooti Farangi


2 Answers

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).

like image 60
Jakub Konecki Avatar answered Oct 15 '22 00:10

Jakub Konecki


Can't really say that there is some advantage of using one over the other. DisplayName exists in older versions of the framework while Display can be used only in ASP.NET MVC 3 as it was introduced in .NET 4.0.

like image 33
Darin Dimitrov Avatar answered Oct 15 '22 01:10

Darin Dimitrov