ASP.NET MVC3 Code First Scaffolding | List view does not comply with [Display (Name = "...")] attribute

I tried to decorate the POCO class [Display(Name="First Name")] As it should ...

  public int Id { get; set; } [Display(Name = "First Name")] public string FirstName { get; set; } [Display(Name = "Last Name")] public string LastName { get; set; } 

And also using the attribute [DisplayName("First Name")] .

Regardless, the default List view (using the Add Controller dialog box) generates a table with property names (for example, "FirstName") as header text without respecting attribute values. Creating a view works fine with the [Display(Name=...)] attribute.

The List.tt T4 template has:

  <th> <#= property.AssociationName #> </th> 

whereas the Create.tt template has:

  <# if (property.IsForeignKey) { #> @Html.LabelFor(model => model.<#= property.Name #>, "<#= property.AssociationName #>") <# } else { #> @Html.LabelFor(model => model.<#= property.Name #>) <# } #> 

Is there anything else you can do to make the scaffolding use the Display attribute by default? Or do I need to modify the List.tt T4 template to use something else than <# property.AssociationName #> ?

Of course, I can edit the created view. But I like to โ€œfixโ€ this in the template itself, so ALL created views will be correct without change.

Thanks in advance for your answers.

+4
source share
1 answer

The Display Data Annotation attribute is only performed by the Html.LabelFor method, since your List.tt only "prints out" the specific AssociationName for each property, which is what you actually get.

If you want to print the Display name for each property, you will also have to spit out the LabelFor method, as in your Create.tt :

 @Html.LabelFor(model => model.<#= property.Name #>) 

Or even better, just copy it all there, just like in the case when the property is a foreign key.

+5
source

All Articles