结合MVC的WebGrid列源的字典属性值 [英] Binding MVC WebGrid column to source's dictionary property value

查看:117
本文介绍了结合MVC的WebGrid列源的字典属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC的整个的WebGrid我的应用程序,它一直伟大的工作。我设置网格列在我的控制器code,然后轻描淡写地说我的看法。我添加了一个新列这样的,其中列表< MvcWebGridColumn>

I'm using MVC's WebGrid throughout my application and it's been working great. I'm setting the columns for the grid in my controller code and then passing that to my view. I add a new column like this where columns is a List<MvcWebGridColumn>:

columns.Add(new WebGridColumn { ColumnName = "Path", Header = "Full Path", Filter = ... });

然后在我看来,我设置从我的视图模型网格列如下:

And then in my view, I set the grid columns from my view model like this:

@grid.GetHtml(
    footerStyle: "hide",
    mode: WebGridPagerModes.All,
    htmlAttributes: new { id = @Model.Id },
    columns: @Model.Columns)

直到此时,设置列在code已经为列已经知道我的车型,第一级属性伟大的工作。现在,事情比较复杂,我不知道如何使用的WebGrid处理。下面是一个例子模型:

Up until this point, setting the columns in the code has worked great as the columns have been known, first-level properties of my models. Now things are more complicated and I don't know how to handle it with the WebGrid. Here is an example model:

public class Requirement
{
    public string Id { get; set; }
    public string Path { get; set; }
    public Dictionary<string, string> Fields { get; set; }
}

我希望能够使用从字段字典财产的主要价值在code设置的WebGrid的专栏。我想这一点,但它不工作:

I want to be able to set a WebGrid's column in the code using the value from the Field dictionary property's key. I'm trying this, but it's not working:

columns.Add(new WebGridColumn { ColumnName = String.Format("Fields[\"{0}\"]", key), Header = label });

我试过也把一个方法 GetFieldValue(字符串键)方法对我的要求模式,但它不 ŧ喜欢。现在看来似乎只能处理精确匹配对象的属性标识符。

I tried also putting a method GetFieldValue(string key) method on my Requirement model, but it doesn't like that either. It seems like it can only handle exactly matching an object's property identifier.

我不知道这是可能使用的WebGrid开箱或者如果我必须扩展功能。先谢谢了。

I'm not sure if this is possible using WebGrid out of the box or if I have to extend the functionality. Thanks in advance.

推荐答案

我相信我只是想出了一个方法来处理这​​似乎并没有太哈克。

I believe I just came up with a way to handle this that doesn't seem too hacky.

要重申这个问题,我相信MVC的WebGrid使用反射来设置的ColumnName 属性让你的输入字符串需要通过对象的API,比如直接访问。 IDField.Address.Name等在我的情况的问题是,我的价值需要为我列的列来自字典,它似乎并不像你可以传递一个参数(键,索引等)到的ColumnName 来得到我所需要的值

To restate the problem, I believe the MVC WebGrid uses reflection to set the ColumnName property so the string you enter for that needs to be directly accessible via the object's API eg. "Id", "Field.Address.Name", etc. The problem in my case is that the value I need for my column's row comes from a Dictionary and it doesn't seem like you can pass a parameter (key, index, etc) into the ColumnName to get the value I need.

我的解决办法是避免解析值时要显示使用的WebGrid的的ColumnName 属性完全。我只是输入电网模型的值,我知道存在的的ColumnName ,在我的情况ID。这只是用来使的WebGrid将呈现。然后,由于格式的WebGridColumn的使用动态功能,我可以使用任何我想要的定义我的列值:

My solution is to avoid using the ColumnName property of the WebGrid entirely when resolving the value to be displayed. I simply enter a value for the grid's model that I know exists for the ColumnName, in my case "Id". This is just used so the WebGrid will render. Then, since the Format of the WebGridColumn uses a dynamic function, I can define my column value using whatever I want:

columns.Add(new WebGridColumn
{
    ColumnName = "Id",
    Header = field.Label,
    Format = x => new HtmlString(x.Value.GetEntityFieldValue(field.Field))
});

我很快意识到,这会搞乱排序。 MVC的WebGrid的黑箱排序只需使用的ColumnName在排序查询,所以对我来说每一列目前拥有?排序= ID。如果你不排序,那么你就OK。如果你是,你必须实现一个自定义排序的解决方案就像我目前做的事情。

I soon realized that this will mess up sorting. MVC WebGrid's black-box sort simply uses the ColumnName in the sort query so every column for me now has "?sort=Id". If you're not sorting, then you're OK. If you are, you'll have to implement a custom sorting solution like I'm currently doing.

这篇关于结合MVC的WebGrid列源的字典属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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