Kendo 网格,数据表绑定:System.ArgumentException:具有指定名称的 属性:无法在类型上找到 ID:System.Data.DataRowView

Kendo Grid, datatable bind: System.ArgumentException: Property with specified name: ID cannot be found on type: System.Data.DataRowView

在我看来,我发送模型,其中一个 属性 是数据表(称为数据),我将其绑定到 Kendo 网格:

@(Html.Kendo().Grid(Model.Data)
   .Name("MyGrid")
   .Columns(c =>
   {
       if (Model.Data != null)
       {
           int index = 0;
           foreach (System.Data.DataColumn column in Model.Data.Columns)
           {
               var col = c.Bound(column.ColumnName).Width("300px").EditorTemplateName("String");
               if (index < 2)
               {
                   col.Locked(true).Lockable(false);
               }
               index++;
           }
       }
   })
   .DataSource(dataSource => dataSource
     .Ajax()
    .Batch(true)
    .ServerOperation(false)
    .Model(model =>
    {
        model.Id(p => p.Row[0]);
        if (Model.Data != null)
        {
            model.Id(Model.Data.Columns[0].ColumnName);
            int index = 0;
            foreach (System.Data.DataColumn column in Model.Data.Columns)
            {
                var field = model.Field(column.ColumnName, column.DataType);
                 if (index < 2)
               {
                   field.Editable(false);
               }
               index++;
            }
        }
    })
    .PageSize(20)
.Create(update => update.Action("Create", "Home"))
.Update(up => up.Action("UpdateRow", "Home"))
)  
.Editable(editable => editable
    .Mode(GridEditMode.InCell)
    )
    .Selectable(selectable =>
    {
        selectable.Mode(GridSelectionMode.Single);
        selectable.Type(GridSelectionType.Row);
    })
.Scrollable(scr => scr.Height(500))
.Reorderable(reorder => reorder.Columns(true))
.Resizable(r => r.Columns(true))
.Filterable(ftb => ftb.Mode(GridFilterMode.Row))
.HtmlAttributes(new { style = "height:100%" })
.Pageable()
.Navigatable()
.Sortable()
.Groupable(g => g.ShowFooter(true))
    )

问题出在第 field.Editable(false) 行 - 当我尝试设置为 true 时它起作用了。它说我的 DataRowView 中没有 ID 值 - 我很确定我的 DataTable 有名为 ID 的列,我检查了 100 多次。 确切错误:

Property with specified name: ID cannot be found on type: System.Data.DataRowView Parameter name: propertyName

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: Property with specified name: ID cannot be found on type: System.Data.DataRowView Parameter name: propertyName

我遇到了这个问题。 我用过

.Events(events =>
    {
        events.Edit("edit_handler");
    })

我的函数看起来在哪里:

<script type="text/javascript" language="javascript">

        function edit_handler(e) {
            var grid = $('#MyGrid').data('kendoGrid');
            var fieldName = grid.columns[e.container.index()].field;
            var columnsToEdit = document.getElementById('EditableColumns').textContent;
            var arrayOfColumns = columnsToEdit.split(",");
            if (arrayOfColumns.indexOf(fieldName.toUpperCase()) < 0) {
                $("#MyGrid").data("kendoGrid").closeCell(e.container);
            }
        }
    </script>

其中 "EditableColumns" 标签包含应可编辑的所有列名称。