How to Pass Additional Data With the DataSourceResult Back to the View
Environment
Product | Telerik UI for ASP.NET Core |
Description
How can I pass additional data with the DataSourceResult from the Controller back to the View?
Solution
The desired result can be achieved by following the steps below:
- Store the output of the ToDataSourceResult method call in a variable.
- Return a custom Json result with the same field names and add the additional fields
- Add a
RequestEnd
event handler to retrieve the additional data in the View
var resultFinal = result.ToDataSourceResult(request);
return Json(new
{
Data = resultFinal.Data,
Total = resultFinal.Total,
AggregateResults = resultFinal.AggregateResults,
Errors = resultFinal.Errors,
myAdditionalParam= "additional data", // Add the extra value
});
.DataSource(dataSource => dataSource
.Events(ev=>ev.RequestEnd("requestEnd"))
)
<script>
function requestEnd(e){
//access the additional data sent from the Controller
console.log(e.response.myAdditionalParam);
}
</script>