This question is what I faced when I'm trying to build CRUD with Grid,
I got multiple grids in one view, and each grid shows different infos depends on the model,
I'll use a simple model as example:
Model1:
public class Model1 { public string Name { get; set; } public int Age { get; set; } }
What Ive tried already, I can pass Model1 with IQueryable data to view like below when there is only one model
public IActionResult Data_Read([DataSourceRequest] DataSourceRequest request)
{
IQueryable<Model1> datas = Model1 that I Queried
DataSourceResult dataresult = datas.ToDataSourceResult(request);
return Json(dataresult);
}
I'm guessing the view model would be something like this:
public class MultipleModelViewModel
{
public IQueryable<Model1> Model1 { get; set; }
public IQueryable<Model2> Model2 { get; set; }
}
But when I tried to do as what I imagined,
I was not able to use the extension method ".ToDataSourceResult(request)" with a View Model
public IActionResult Data_Read([DataSourceRequest] DataSourceRequest request)
{
IQueryable<Model1> datas = Model1 that I Queried
MultipleModelViewModel.Model1 = datas;
//can't use the ".ToDataSourceResult" extension method with the ViewModel
DataSourceResult dataresult = MultipleModelViewModel.ToDataSourceResult(request);
return Json(dataresult);
}
The other thing I'm assuming is, this view model should be work via CRUD operations,
since the grid is counting on the view model to bind the datas,
and so I'm guessing that when client creates a row of data,
the new data should be passed back to the "Create" action via the view model,
Dont know if I am right,
Can anyone help?