4

Objective: I want to pass an object of type Kendo.Mvc.UI.DataSourceRequest to the Mvc action, so that i can get the results from database according to the sorting and filtering applied.

Problem/Obstacle: The object gets null when it reaches to the action.

MY Controller Action

    public ActionResult Getresults([DataSourceRequest]DataSourceRequest request, Int32 TotalRec)
    {
        try
        {
            //get data from DAL
            var result = new DataSourceResult()
            {
                Data = List, // Process data (paging and sorting applied)
                Total = TotalRec
            };
            return Json(result, JsonRequestBehavior.AllowGet);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

My jquery Function

function getData() {
    var gridDatasource = $('#gridname').data('kendoGrid').dataSource.options;
    var datatoPost = "{'request': '" + JSON.stringify(new kendo.data.DataSource(gridDatasource)) + "', 'TotalRec': '" + 100 + "'}";
    //new kendo.data.DataSource
    $.ajax({
        type: "Post",
        url: '/Administrator/Getresults/',
        contentType: "application/json; charset=utf-8",
        data: datatoPost,
        dataType: "json",
        processdata: false,
        success: function (value) {
            alert(value.d);
        },
        error: function () { alert("Ajax Error"); }
    });
}

I tried to JSON.stringify but still the same and also like var datatoPost = "{'request': '" + JSON.stringify(gridDatasource) + "', 'TotalRec': '" + 100 + "'}";

Do i need to parse my object here or may be convert its type.

  • Not sure if this helps but KendoUI wrappers are really helpful, as they render the required javascript on to the page. You should use these wrapper libraries. Check the demos in this [link](http://www.kendoui.com/server-wrappers.aspx) – Nilesh Sep 13 '13 at 06:38
  • I am also facing similar problem. – Bhupendra Shukla Sep 13 '13 at 06:42
  • 1
    For the record, solution provided by @spottedone works like a charm. – ken2k Jul 09 '14 at 12:34

1 Answers1

9

For me worked the following:

$("#excel").kendoButton({
  click: function (event) {

    var data = grid.dataSource._params();
    var prepared = grid.dataSource.transport.parameterMap(data);

    $.post("/Root/AnotherControllerMethod", prepared, 
       function (data, status, xhr) {
         console.log("Ok!");
       }
    );
  }
});
spottedone
  • 136
  • 1
  • 5