ASP.NET MVC3 Code First Scaffolding | List view doesn't honor the [Display(Name="...")] attribute
I tried decorating the POCO class with [Display(Name="First Name")]
Like follows...
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 [DisplayName("First Name")]
attribute.
No matter what, the 开发者_高级运维default "List" view generated (using the "Add Controller" dialog box) always generates the table with the property names (like "FirstName") as the header text without honoring the attribute values. Create view works fine with the [Display(Name=...)]
attribute though.
The List.tt
T4 template actually 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 to do to make the default scaffolding use the Display
attribute? Or should I edit the List.tt
T4 template to use something else than <# property.AssociationName #>
?
Of course I can edit the generated view. But I like to 'fix' this in the template itself so that ALL the views generated will be correct without modifying.
Thanks in advance for the answers.
The Display
Data Annotation attribute is only honored by the Html.LabelFor
method, so as your List.tt
only "prints out" the particular AssociationName
for each property, that's what you actually get.
If you want it to print out the Display
name for each property, you will also have to spit out a LabelFor method there, just like in your Create.tt
:
@Html.LabelFor(model => model.<#= property.Name #>)
Or even better, just copy the whole thing there, so the case when the property is a foreign key is also handled.
精彩评论