所以我花了很多时间用Required、StringLength、Range、Display等注释我的域实体。
然后我进入并开始创建具有正确域实体集的强类型 View 。
我尝试的第一个 View 是我的域实体的 List Scaffolded View ,该 View 慷慨地应用了 Display(Name="foo") 属性。创建 View 时,表头仍然使用属性名称,而不是我设置的显示名称。
带有 April Tools 更新的 MVC3 是否会忽略这些数据注释?
最佳答案
假设您正在使用 MvcScaffolding ,那么是的,这些脚手架模板会忽略元数据。
但是如果你关注 this article您可以创建自己的模板。完成后,您可以修改自己的模板。
在模板中查找此部分
<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>
改
<th><#= property.Name #></th>
至 <th>@Html.LabelFor(x => x.First().<#= property.Name #>)</th>
它将改用模型元数据显示名称。请注意,这只是为了快速说明如何根据索引 View 的模板(具有 IEnumerable<> 作为模型)来处理此问题。
关于asp.net-mvc - 为什么添加 View 列表脚手架向导不使用 "DisplayName"数据注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7396181/