asp.net-mvc - 为什么添加 View 列表脚手架向导不使用 "DisplayName"数据注释

标签 asp.net-mvc

所以我花了很多时间用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/

相关文章:

jquery - ASP.NET MVC 5 中的 Ajax(部分 View )不起作用 - 始终仅单独打开部分 View

c# - Ninject 的 Httpcontext.Session 始终为 null

asp.net-mvc - 如何在不包含 MicrosoftAjax.js 的情况下使用 MicrosoftMvcValidation.js?

c# - BeginForm 生成一个带有 id 的表单操作

asp.net-mvc - godaddy 中等信任级别下的 Entity Framework 6 和 System.Security.Permissions.ReflectionPermission

asp.net - 'ASP.NET Web Application' 中的动态编译可能吗?

c# - 配置 Asp.net MVC 项目和 EntityFramework 以使用 Redis 作为缓存提供程序

css - 实现管理布局 NopCommerce?

asp.net-mvc - ASP.NET MVC 用户友好的 401 错误

c# - 使用 dotnetzip 压缩文件,打开时文件已损坏