5
votes

When defining a DataSource within @(Html.Kendo().Grid(Model), I have successfully used

.DataSource( dataSource => .Ajax( ).Model( model => model.Id( m => m.PROPERTY ) ) )

where PROPERTY is a property of the object that is the model. What is the correct syntax for defining Model.Id if the Model is a System.Data.DataTable and the Id column in the DataTable is TableId?

In other words, model.Id( m => ??? ).

I have tried, model.Id( m => Model.PrimaryKey ), which seems to satisfy the requirement that model.Id be set, but the Update Action (.Update(update => update.Action("MyUpdateMethod", "MyController")) doesn't ever hit, so I think there must still be something wrong.

2
I didn't even know you could bind it to a DataTable. If no one has an idea on how to do it, you may just need to define it in JavaScript instead of using an MVC helper.CodingWithSpike
@CodingWithSpike, could you point me to a sample?Kelly Cline
In plain JavaScript you would set new kendo.data.DataSource({ schema: { model: { id: "PrimaryKey" } } }); if you wanted to convert to JS instead of MVC helper, the easiest thing to do is use the MVC helper once, view the page in your browser, and copy/paste the generated JS code. Then replace your MVC helper razor code with the generated JS.CodingWithSpike

2 Answers

2
votes

You can bind to a DataTable. In fact, we do a lot of dynamic grids and DataTable is our only recourse. Binding is a little different, though.

A snippet for one of ours is like this:

@model System.Data.DataTable
@(Html.Kendo().Grid(Model)
    .Name("SomeGrid")
    .Columns(columns=>
      { 
          foreach(System.Data.DataColumn column in Model.Columns)
          {
              columns.Bound(column.ColumnName).Title(column.Caption).Width(200);
          }
       }
    )
    .Selectable(selectable=>selectable
       .Type(GridSelectionType.Row)
    )
    .DataSource(dataSource => dataSource
        .Ajax()
        .Model(model =>
             {
                 foreach(System.Data.DataColumn column in Model.Columns)
                 { 
                     model.Field(column.ColumnName,column.DataType);
                 }
             }
         )
         .Read(read=>Action("SomeMethod", "SomeController"))
         .PageSize(20)
     )
0
votes

I have an answer, although it's not real slick. There are several parts to it.

First, in order to Edit, I must set a Model.Id, how to do which was my original question. The answer to that is simply, model.Id( "KEY_COLUMN_NAME" ) in the .DataSource.Model method. However, that is not enough to solve the problem that Update does not occur. Telerik Support suggested that I needed to define a custom data object (POCO) that had all the same properties as the DataTable has columns. That POCO becomes the type for the update

public ActionResult MyUpdate( [DataSourceRequest] DataSourceRequest _request, MyPOCO _data ){...}

The Editor can then map the DataRow columns to the POCO properties, and then MyUpdate can work.