2

I have the following method in my ASP.NET Core Controller

public async Task<JsonResult> MyJsonResultMethod(int page,int take, int skip, IEnumerable<Sort> sort){

...

public class Sort
{
    public string field { get; set; }
    public string dir { get; set; }
}

My javascript on the front end Kendo UI Web grid does a get controller method

$("#grid").kendoGrid({
    dataSource: {
        type: "json",
        transport: {
            read: "/MyController/MyJsonResultMethod"
        },
        pageSize: 5,
        serverPaging: true,
        serverSorting: true
    },
    height: 550,
    sortable: true,

    pageable: {
        refresh: true,
        pageSizes: true,
        buttonCount: 5
    },

If I monitor it in fiddler I can catch the HTTPGet and see all the values are passed

GET http://localhost:62594/MyController/MyJsonResultMethod?take=5&skip=0&page=1&pageSize=5&sort%5B0%5D%5Bfield%5D=DocumentNumber&sort%5B0%5D%5Bdir%5D=asc

enter image description here When I debug my controller method I can see all the properties passed to the method have the correct values apart from the IEnumerable<Sort> sort which is received as null. I tried changing it to Sort[] sort but result is still the same

From my understanding this is unique to asp.net core and they way kendo ui sends an array. I logged the call with Telerik support says the Kendo UI Web grid doesn't support asp.net core. I can't see the reason for this as Fiddler clearly sends the data. I just need to somehow get the parameter to support it.

dfmetro
  • 4,462
  • 8
  • 39
  • 65

1 Answers1

0

I'm not sure the "sort[0][field]" is formatted properly for ASP.Core model binder to properly interpret it.

This is how I format and pass array data to a controller and get it to bind(in ASP.NET MVC, not Core).

Request data:

sort[0].field: DocumentNumber
sort[0].dir: asc
sort[1].field: SecondSortField
sort[1].dir: asc

Controller Action:

public async Task<JsonResult> MyJsonResultMethod(
    int page,
    int take,
    int skip,
    [Bind(Prefix = "sort")]IEnumerable<Sort> sort)

This formatting and action using [Bind] is the same way that the Kendo Grid passes the new/updated/deleted arrays to the server so I "copied" the format it when I needed to pass my own array and it works for me.

The Dread Pirate Stephen
  • 3,089
  • 1
  • 12
  • 14
  • I added the Bind attribute and the result is still the same. From my understanding this is unique to asp.net core and they way kendo ui sends an array. – dfmetro Mar 03 '17 at 16:22
  • Possibly...but also in my/kendo's way, the data is sent as "sort[0].field" *not* "sort[0][field]"...I would still try formatting your request data with dot-notation in addition to the Bind attribute. – The Dread Pirate Stephen Mar 04 '17 at 04:13