我尝试用 [Display(Name="First Name")]
装饰 POCO 类喜欢如下...
public int Id { get; set; }
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
并且还使用
[DisplayName("First Name")]
属性。无论如何,生成的默认“列表” View (使用“添加 Controller ”对话框)总是生成带有属性名称(如“名字”)作为标题文本的表,而不遵守属性值。使用
[Display(Name=...)]
创建 View 工作正常属性虽然。List.tt
T4模板实际上有: <th>
<#= property.AssociationName #>
</th>
而
Create.tt
模板有: <#
if (property.IsForeignKey) {
#>
@Html.LabelFor(model => model.<#= property.Name #>, "<#= property.AssociationName #>")
<#
} else {
#>
@Html.LabelFor(model => model.<#= property.Name #>)
<#
}
#>
还有什么办法可以让默认脚手架使用
Display
属性?或者我应该编辑 List.tt
T4 模板使用除 <# property.AssociationName #>
以外的其他内容?当然我可以编辑生成的 View 。但是我喜欢在模板本身中“修复”这个问题,这样生成的所有 View 都将是正确的而无需修改。
预先感谢您的回答。
最佳答案
Display
数据注释属性仅受 Html.LabelFor
支持方法,所以你的List.tt
只“打印出”特定的 AssociationName
对于每个属性,这就是您实际得到的。
如果您希望它打印出 Display
每个属性的名称,您还必须在那里吐出一个 LabelFor 方法,就像在您的 Create.tt
中一样:
@Html.LabelFor(model => model.<#= property.Name #>)
或者更好的是,只需将整个内容复制到那里,这样属性是外键的情况也会被处理。
关于ASP.NET MVC3 代码优先脚手架 | ListView 不支持 [Display(Name ="...")] 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6009467/