asp.net – 如何在WebGrid中的列标题使用DisplayName数据注释?

我有一个Car类,我试图使用WebGrid帮助器在MVC 3视图中显示.下面是Car和它的元数据类.

汽车类:

[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",?????)
    ));

目标:我想知道如何使用displayName数据注释作为WebGrid(?????)中的列标题文本.有谁知道这是如何实现的?

提前致谢!

解决方法

丑陋的地狱,但它可以工作:
grid.Column(
    "CarName",ModelMetadata.FromLambdaExpression(
        car => car.CarName,new ViewDataDictionary<Car>(new Car())
    ).displayName
)

问题是,WebGrid帮助器完全基于动态数据,绝对没有强大的打字,这是我讨厌它的原因之一. Microsoft的WebMatrix团队必须是C#4.0动态功能的真正粉丝,因为他们的整个API只需要弱类型的对象:-)

MvcContrib Grid好多了

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...