简体   繁体   中英

Kendo UI - Ajax MVC - JSON always null

I have a Kendo grid that has been sort-enabled. I want to do an ajax postback using jQuery to send the sort information to the action method to do some action.

var datasource = $(".data-table").data("kendoGrid").dataSource;
$.ajax({
    type: 'POST',
    url: '@Url.Action("ExportToPDf", "MyController")',
    dataType: 'json',
    data: { sort: datasource._sort } 
});

I'm able to see with a debugger that the correct value is got and passed in the data attribute of the ajax. I used FireBug to confirm that the values are passed during the POST action.

public ActionResult ExportToPDf(List<SortDescription> sort)
{
    //Will be doing some action
    return null;
}

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

Sample data from Firebug during POST action

sort[0][dir]    asc
sort[0][field]  EmployeeRef

When I keep breakpoint in action method, im able to get one item in the list, but the properties appear to be null.

Can anyone please guide me what I do wrong?

Try something like this:

$.ajax({
    url: '@Url.Action("ExportToPDf", "MyController")',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify({sort: datasource._sort })
 })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM