This is a migrated thread and some comments may be shown as answers.

How to map grid to result with extra Json objects

1 Answer 331 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Luis
Top achievements
Rank 1
Luis asked on 17 Jun 2016, 10:54 PM

Hello I have a kendo grid in MVC which currently works fine. I am making an Ajax request to read the information on the grid and everything works fine. However, I want to send another object into the same json coming from my action result that populates the grid as follows:

public JsonResult ReadElements([DataSourceRequest]DataSourceRequest request)      
{
    // this is my data that i want to populate in the grid
    var result = GetMyGridElements().ToDataSourceResult(request);
    //returning the following line allows my grid to be populated correctly
    //return Json(result, JsonRequestBehavior.AllowGet);
    
   var otherInfo = GetOtherInfo();
   //i want to return something like the following:
   return Json(new {result, otherInfo}, JsonRequestBehavior.AllowGet);
}

However, this results in a grid error. I have tried to bind to the following events to see what I could accomplish:

.Events(e => e.DataBinding("MyGrid.DataBinding"))

and

.Events(e => e.RequestEnd("MyGrid.RequestEnd"))

which use the following defined functions:

MyGrid.DataBinding = function (e) {
    debugger;
}
 
MyGrid.RequestEnd = function(e) {
    debugger;
}

I see that the only difference on the request end function is the way the e.response object behaves. On the non-working example the e.response object has a result and a otherInfo properties, whereas in the working example the content of the e.response object match the contents of the e.response.result object. So I tried doing this:

MyGrid.RequestEnd = function(e) {
    e.response = e.response.result;
}

However, on the databinding event, the e.items object is empty on the non-working scenario and contains all elements in the working scenario and my grid does not display any element

Basically my question is: 

is there a way that these two events connect with each other? how can I programatically select which information will be bound to the grid when multiple information is present on the response of the ajax request?

 

1 Answer, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 21 Jun 2016, 02:40 PM

Hello Luis,

There is a possibility to return a new custom object after the linq expressions are applied against the IQueryable. Please refer to the following overload method of  ToDataSourceResult:

  

return Json(entities.ToDataSourceResult(request, ModelState, product => new ProductViewModel
       {
           ProductID = product.ProductID,
           ProductName = product.ProductName,
           UnitsInStock = product.UnitsInStock
       }));

It allows to create a custom field to each object of the data returned by the server.


Regards,
Boyan Dimitrov
Telerik
 
Get started with Kendo UI in days. Online training courses help you quickly implement components into your apps.
 
Tags
Grid
Asked by
Luis
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Share this question
or