为什么我的 KendoUI 网格无法在我的 ASP.NET Core 应用程序中排序?



当我尝试对网格进行排序时,我会得到以下错误:

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:"object"不包含"ColumnName"的定义

在这个特定的设置中,我所做的是将所有数据库表列名返回到类型为objectList,并将其返回到数据源。然后,我创建了一个伪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);
}

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

根据模式/模型,您的模型只有字段"Id"。您还需要定义字段ColumnName和ColumnType。

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

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

由于使用了模型,列表类型也必须更改以适应它。因此,我将其从List<object>更改为List<ModelClass>,如下所示:

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

最新更新