开发者

Why doesn't the Add View List Scaffolding wizard use the "DisplayName" data annotation

So I just spent a bunch of time annotating my domain entities with Required, StringLength, Range, Display etc.

I then went in and started creating my strongly typed views that have the correct domain entity set.

The first view I tried was a List Scaffolded view for my domain entity that has Display(Name="foo") attributes generously applied. When the view is created, the table headers still use the property name, rather than the displayname I set.

Doe开发者_Python百科s MVC3 with April Tools update ignore these data annotations?


Assuming you are using MvcScaffolding, then yes, these scaffolding templates ignore metadata.

But if you follow this article you can create you own template. When you have done that, you can modify your own template.

Look for this part in the template

<thead>
    <tr>
        <th></th>
<#
List<ModelProperty> properties = GetModelProperties(Model.ViewDataType, true);
foreach (ModelProperty property in properties) {
if (!property.IsPrimaryKey && !property.IsForeignKey && !property.IsEnumerable) {
#>
        <th><#=  property.Name #></th>
<#
}
}
#>
    </tr>
</thead>

change <th><#= property.Name #></th> to <th>@Html.LabelFor(x => x.First().<#= property.Name #>)</th> and it will use the modelmetadata displayname instead. Be cautious though that this it just to illustrate quickly how you could handle this instead, based on the template for the index view (which has an IEnumerable<> as a model).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜