FirstDivision February 2016

Kendo UI Grid DataSourceRequest and DataSourceRequestModelBinder in MVC6

There seems to be something going wrong when the DataSourceRequestModelBinder runs and tries to create a DataSourceRequest on the parameters sent in by the Grid. This is in MVC 6 with Kendo.Mvc v2016.1.112.

On the cshtml side, the Kendo DataSource looks like this:

var myDS = new kendo.data.DataSource({
    serverPaging: true,
    serverSorting: true,
    pageSize: 3,
    transport: {
        read: {
            url: "/api/kendotest/GetKendoGridData"
        }
    },
    schema: {
        data: "Data",
        total: "Total"
    }
});

And the controller looks like this:

[Route("GetKendoGridData")]
public DataSourceResult GetKendoGridData([DataSourceRequest]DataSourceRequest request)
{
    //create some sample objects
    var ids = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    var data = ids.Select(i => new MyModel
    {
        Id = i
    });

    return data.AsQueryable().ToDataSourceResult(request);
}

The initial read works fine, and paging works fine, but server side sorts do not. I think this is because of the way the Kendo grid send in the sort parameters and results in the DataSourceRequestModelBinder not being able to parse it out. I started to write my own ModelBinder but then decided to try to figure this out one more time.

It comes close when trying to supply sort parameters - the ModelBinder populates the Sort object, but the "Member" field is always null: No sort for you

Answers


Marco Palma February 2016

Probably this can happen if kendo.aspnetmvc.min.js is not included in your page.

Check this is usefull.

Hope this help


FirstDivision February 2016

This appears to have been solved by adding type: "webapi", to my datasource. So it would end up looking like this:

var myDS = new kendo.data.DataSource({
    serverPaging: true,
    serverSorting: true,
    pageSize: 3,
    type: "webapi",
    transport: {
        read: {
            url: "/api/kendotest/GetKendoGridData"
        }
    },
    schema: {
        data: "Data",
        total: "Total"
    }
});

Post Status

Asked in February 2016
Viewed 2,455 times
Voted 7
Answered 2 times

Search




Leave an answer