ASP.NET MVC3的WebGrid助手和模型元数据 [英] ASP.NET MVC3 WebGrid Helper and Model Metadata

查看:122
本文介绍了ASP.NET MVC3的WebGrid助手和模型元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的WebGrid HTML帮手ASP.NET MVC 3根据在ModelMetadata中的信息自动生成列。例如,在视图中的code,接受对象的列表是:

I'm trying to use the WebGrid html helper in ASP.NET MVC 3 to autogenerate the columns according to the information found in the ModelMetadata. For example the code in a view that accepts a list of objects would be:

var grid = new WebGrid(Model);
@grid.GetHtml(columns: ViewData.ModelMetadata.Properties.Single.Properties
               .Select(p => grid.Column(
                       columnName: p.PropertyName,
                       header: p.ShortDisplayName
               )));

这实际上就像一个魅力(我很惊讶,这是实际上容易)。这里发生的是,从模型的属性我用ShortDisplayName作为列的标题。

This actually works like a charm (I was surprised it was that easy actually). What happens here is that from the properties of the model I use the ShortDisplayName as the column's header.

问题?我需要的默认格式适用于所有列。基本上,我想用Html.Raw扩展为所有我的网格将显示数据。尝试会是这样的:

The problem? I need to apply a default format to all columns. Basically I want to use the Html.Raw extension for all the data that my grid will display. An attempt would be something like that :

var grid = new WebGrid(Model);
@grid.GetHtml(columns: ViewData.ModelMetadata.Properties.Single.Properties
               .Select(p => grid.Column(
                       columnName: p.PropertyName,
                       header: p.ShortDisplayName,
                       format: (item) => Html.Raw(GetPropertyValue(item, p.PropertyName))
               )));

其中,方法GetPropertyValue会使用反射或任何(我需要在这里提醒该项目是动态的,它的价值实际上是被显示在当前行中的对象)读取属性的值。

where the method GetPropertyValue would read the value of the property using reflection or whatever (I need to remind here that item is dynamic and its value is actually the object that is being displayed in the current row).

有没有什么更好的方法来做到这一点?

Is there any better way to do this?

谢谢,

科斯塔斯·

推荐答案

我建议你寻找到MVCContrib电网项目:的 HTTP://mvccontrib.$c$cplex.com/wikipage标题=电网

I suggest you looking into MVCContrib Grid project: http://mvccontrib.codeplex.com/wikipage?title=Grid

这篇关于ASP.NET MVC3的WebGrid助手和模型元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆