如何使用在的WebGrid列标题显示名称数据注解? [英] How can I use DisplayName data annotations for column headers in WebGrid?
问题描述
我有我想要在使用的WebGrid助手的MVC 3视图显示汽车类。下面是汽车和它的元数据类。
I have a Car class that I'm trying to display in an MVC 3 view using the WebGrid helper. Below are the Car and it's metadata class.
车类:
[MetadataType(typeof(CarMetadata))]
public partial class Car
{
// car implementation
}
租车元数据类:
public class CarMetadata
{
[DisplayName("Car Name")]
[StringLength(100, ErrorMessageResourceType = typeof(ValidationText), ErrorMessageResourceName="CarNameDescriptionLength")]
[Required]
public string CarName { get; set; }
}
查看内容:
@model List<Car>
...
var grid = new WebGrid(Model, canPage: true, rowsPerPage: 10);
grid.Pager(WebGridPagerModes.NextPrevious);
@grid.GetHtml(
htmlAttributes: new { id = "grid" },
columns: grid.Columns(
grid.Column("CarName", ?????)
));
目标:我想弄清楚如何使用数据显示名称注释作为的WebGrid列标题的文本( ????? )。有谁知道这是如何实现的?
GOAL: I'd like to figure out how to use the DisplayName data annotation as the column header text in the WebGrid (?????). Does anyone know how this is accomplished?
在此先感谢!
推荐答案
丑陋不堪,地狱,但它可以工作:
Ugly as hell but it could work:
grid.Column(
"CarName",
ModelMetadata.FromLambdaExpression(
car => car.CarName,
new ViewDataDictionary<Car>(new Car())
).DisplayName
)
问题是,在助手的WebGrid完全是基于动态数据,绝对没有强类型,这就是为什么我讨厌它的原因之一。该WebMatrix的团队在微软必须在C#4.0的动态功能的真正的球迷为他们的整个API仅需弱类型的对象: - )
The problem is that the WebGrid helper is entirely based on dynamic data, absolutely no strong typing and that's one of the reasons why I hate it. The WebMatrix team at Microsoft must be real fans of the C# 4.0 dynamic feature as their entire API takes only weakly typed objects :-)
MvcContrib电网是好多了。
这篇关于如何使用在的WebGrid列标题显示名称数据注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!