如何在 WebGrid 中使用 DisplayName 数据注释作为列标题?
我有一个 Car 类,我试图使用 WebGrid 帮助程序在 MVC 3 视图中显示它。下面是汽车及其元数据类。
汽车类:
[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 中的列标题文本(?????? )。有谁知道这是如何实现的?
提前致谢!
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.
Car class:
[MetadataType(typeof(CarMetadata))]
public partial class Car
{
// car implementation
}
Car metadata class:
public class CarMetadata
{
[DisplayName("Car Name")]
[StringLength(100, ErrorMessageResourceType = typeof(ValidationText), ErrorMessageResourceName="CarNameDescriptionLength")]
[Required]
public string CarName { get; set; }
}
View contents:
@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", ?????)
));
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?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
丑陋不堪,但它可以工作:
问题是 WebGrid 帮助器完全基于动态数据,绝对没有强类型,这就是我讨厌它的原因之一。 Microsoft 的 WebMatrix 团队一定是 C# 4.0 动态功能的真正粉丝,因为他们的整个 API 仅采用弱类型对象:-)
MvcContrib Grid 好多了。
Ugly as hell but it could work:
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 Grid is much better.
我创建了一个像这样的辅助方法:
并像这样使用它:
I have created a helper method like this:
And used it like this: