【问题标题】:Why won't my KendoUI Grid Sort in my ASP.NET Core application?为什么我的 KendoUI Grid Sort 不能在我的 ASP.NET Core 应用程序中?
【发布时间】:2020-09-10 18:11:17
【问题描述】:

当我尝试对网格进行排序时,出现以下错误:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“对象”不包含“列名”的定义

在这个特定的设置中,我所做的是将所有数据库表列名称返回到 object 类型的 object 并将其返回到数据源。然后,我创建了一个伪 Id 列,我希望网格在其架构中用于排序。但是,这并没有什么区别,因为错误告诉我该对象似乎没有要排序的 ColumnName 属性。

这是我的网格的代码。

<kendo-grid name="columnGrid">
    <datasource type="DataSourceTagHelperType.Ajax" page-size="20" server-filtering="true" server-operation="true">
    <transport>
            <read url="/Core/ReadVesselColumns" />
        </transport>
        <schema>
            <model id="Id">
                <fields>
                    <field name="Id" editable="false" />
                </fields>
            </model>
        </schema>
    </datasource>
    <pageable page-size="10" />
    <filterable enabled="true" />
    <sortable enabled="true" />
    <scrollable enabled="true" />
    <columns>
        <column selectable="true" width="50" />
        <column field="ColumnName" title="Column" />
        <column field="ColumnType" title="Type" hidden="true" />                                        
    </columns>
</kendo-grid>

这是数据的操作:

public JsonResult ReadVesselColumns([DataSourceRequest]DataSourceRequest request)
    {
        //Access Entity Type
        var entityType = _context.Model.FindEntityType("Hagland.Data.Vessel");

        //Create a list to store the column names and types
        var columns = new List<object>();
        int colId = new int();

        //Iterate over the results and populate list
        foreach (var property in entityType.GetProperties())
        {
            columns.Add(new
            {
                Id = colId ++,
                ColumnName = property.GetColumnName(),
                ColumnType = property.GetColumnType()
            });
        };            

        //Return list
        var result = columns.ToDataSourceResult(request);
        return Json(result);
    }

通常这不会成为问题,因为我会从数据库中提取结构化数据,但在这种情况下,我需要提取列名并将它们返回到网格以供选择。有什么我做错了吗?我需要重新设置列表吗?

【问题讨论】:

    标签: c# asp.net-core kendo-ui


    【解决方案1】:

    根据架构/模型,您的模型只有字段“Id”。您还需要定义字段 ColumnName 和 ColumnType。

    【讨论】:

      【解决方案2】:

      我发现问题是由于缺少模型来支持它。我试图有点过于抽象,结果网格无法对数据进行排序。解决方案是创建一个模型来匹配我绑定到网格的数据,这样就可以对数据进行排序。

      namespace MyProject.Data
      {
          public class PositionColumns
          {
              public int Id { get; set; }
              public string ColumnName { get; set; }
              public string ColumnType { get; set; }
          }
      }
      

      由于使用了模型,列表类型也必须更改以适应它。所以,我把它从 List&lt;object&gt; 改为 List&lt;ModelClass&gt; 如下:

      var columns = new List<PositionColumns>
                  {
                      new PositionColumns {Id = 1, ColumnName = "Some Name", ColumnType = "string" },
                      ...
                      ...
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-24
        • 2011-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多