Bind two grids from one read action

1 Answer 5 Views
Grid
Richard
Top achievements
Rank 4
Bronze
Bronze
Iron
Richard asked on 17 Jul 2025, 01:53 PM

Good afternoon,

Is there a way to populate a second grid based on the outcome of the read action for the first grid?

For example, the read action for grid1 pulls a list of files from the file system in and runs checks against each file.  The list generated is returned to load grid1.  During the processing a number of errors are generated, creating a second list.  Ultimately I'd like grid2 to display that list.

It seems unnecessary to have a separate action for grid2 that is essentially running the same processing, just returning a different list.

With the two grids having AutoBind set to false I assume I can control the order the girds are populated.  Could grid2 be populated from a list stored in ViewData?

This article explains how two models can be returned to a single View, but ToDataSourceResult in the Controller can only return one model:

https://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/

Kind regards,

Richard

1 Answer, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 4
Bronze
Bronze
Iron
answered on 18 Jul 2025, 12:31 PM

I've come up with a solution that seems to work.

In the Read event for grid1 I store the List of errors in Session:

// Store list file errors
_httpContextAccessor.HttpContext.Session.SetObjectAsJson("errorModels", errorModels);

SetObjectAsJson:

session.SetString(key, JsonConvert.SerializeObject(value));

Grid2 has AutoBind set to false.  In the DataBound event for grid1 I call the Read event for grid2.

var errorGrid = $("#errorGrid").data("kendoGrid");
errorGrid.dataSource.read();

In the Read event for grid2 I retrieve the session list and bind it:

// Retrieve list of file errors
var errorModels = _httpContextAccessor.HttpContext.Session.GetObjectFromJson<List<ErrorModel>>("errorModels");
return Json(errorModels.ToDataSourceResult(request));

GetObjectFromJson:

var value = session.GetString(key);
return value == null ? default : JsonConvert.DeserializeObject<T>(value);

Kind regards,

Richard

Tags
Grid
Asked by
Richard
Top achievements
Rank 4
Bronze
Bronze
Iron
Answers by
Richard
Top achievements
Rank 4
Bronze
Bronze
Iron
Share this question
or