【问题标题】:Telerik MVC grid with List of ExpandoObjectTelerik MVC 网格与 ExpandoObject 列表
【发布时间】:2012-11-02 11:44:41
【问题描述】:

我正在尝试将 Telerik MVC 与动态 ExpandoObjects 的集合一起使用。

控制器是:

[GridAction]
public ActionResult TestDiario()
{
        var result = new List<dynamic>();

        dynamic diarioModel = new ExpandoObject();

        var dictionary = (IDictionary<string, object>)diarioModel;

        dictionary.Add("ID", 1);
        dictionary.Add("AlunoID", 12781);
        dictionary.Add("DAY05CLASS1", true);
        dictionary.Add("DAY05CLASS2", true);
        dictionary.Add("DAY07CLASS1", true);
        dictionary.Add("DAY08CLASS1", true);

        result.Add(diarioModel);
        return View(result);
}

视图是:

@using Telerik.Web.Mvc.UI

@model IEnumerable<dynamic>

@{
    ViewBag.Title = "TestDiario";
}

@(Html.Telerik().Grid(Model).Name("Grid")
    .DataKeys(dataKeys => dataKeys.Add("ID"))
    .Columns(columns => 
    { 
        columns.Bound("MatAnoID").Visible(true);
        columns.Bound("AlunoID");
        columns.Bound("NroClasse");
        columns.Bound("Aluno");

        var dictionary = (IDictionary<string, object>)Model;
        foreach (var property in (IDictionary<String, Object>)dictionary)
        {
            if (property.Key.ToString().Remove(3) == "DAY")
            {
                columns.Bound(property.Key);
            }
        }
    })
    .Pageable()
    .Sortable()
    .Groupable()
    .Filterable()

)

foreach 循环获取以 DAY 字符串开头的动态字段。

当我运行项目时出现以下错误:

{"无法转换类型化对象 'System.Collections.Generic.List1[System.Object]' to type 'System.Collections.Generic.IDictionary2[System.String,System.Object]'。"}

有没有办法使用带有循环字段的 Telerik MVC Control 动态对象?

【问题讨论】:

    标签: asp.net-mvc dynamic telerik telerik-grid


    【解决方案1】:

    是的,你可以,而且你很接近,但犯了一些错误。

    1) 模型不是 IDictionary 类型,因为它不是扩展对象。它是一个动态列表。

    这有点小技巧,但为了解决这个问题,我只取了可枚举中的第一个(或默认)元素,然后用它制作了一个字典。

    2) 您正在尝试绑定在 expando 对象中不作为属性存在的列。

    我把这些注释掉了。

    3) 我认为您正在寻找以“DAY”开头的键。如果你想从列名中删除“DAY”,你可以调整我的示例代码。

    除此之外它工作正常:

    @(Html.Telerik().Grid(Model).Name("Grid")
        .DataKeys(dataKeys => dataKeys.Add("ID"))
        .Columns(columns => 
        { 
            //NOTE: some of these columns are not valid because you didn't include them as properties
            //columns.Bound("MatAnoID").Visible(true);
            //columns.Bound("NroClasse");
            //columns.Bound("Aluno");
    
            columns.Bound("AlunoID");
    
            var first = Model.FirstOrDefault();
            if (first != null) {
                var dictionary = (IDictionary<string, object>)first;
                foreach (var property in dictionary) {
                    string key = property.Key.ToString();
                    if (key.StartsWith("day", StringComparison.InvariantCultureIgnoreCase)) {
                        columns.Bound(property.Key);
                    }
                }
            }
    
        })
        .Pageable()
        .Sortable()
        .Groupable()
        .Filterable()
    )
    

    【讨论】:

      猜你喜欢
      • 2012-05-23
      • 2012-02-19
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多