Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass Object of type `Kendo.Mvc.UI.DataSourceRequest` from jquery to Mvc Action

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.

like image 452
Vinay Pratap Singh Bhadauria Avatar asked Jan 12 '23 09:01

Vinay Pratap Singh Bhadauria


1 Answers

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!");
       }
    );
  }
});
like image 90
spottedone Avatar answered Jan 31 '23 08:01

spottedone