How can I pass a view model with multiple models in it when reading remote data,? (Grid CRUD)

1 Answer 1197 Views
Grid
CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
CHIHPEI asked on 08 Nov 2021, 08:48 AM

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);
        }
The question is how can I pass multiple models with one view model,
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?

1 Answer, 1 is accepted

Sort by
0
Aleksandar
Telerik team
answered on 10 Nov 2021, 11:56 AM

Hi CHIHPEI,

I am not sure I understand exactly the scenario. In general, each Grid can be configured for CRUD functionality and you can refer to the Data binding section for the Grid and the Grid Editing section of the documentation for details on configuration based on the editing mode selected. You can also review the DataSource CRUD section of the documentation

Normally, each Grid will call it's Read endpoint to fetch data:

public ActionResult Products_Read([DataSourceRequest]DataSourceRequest request)
{
    using (var northwind = new SampleContext())
    {
        IQueryable<Product> products = northwind.Products;
        DataSourceResult result = products.ToDataSourceResult(request);
        return Json(result);
    }
}

I suggest also reviewing the demos we have on the remote data binding and editing for the Grid - Grid Demos. Make sure to click on the View Source tab to review the implementation behind the examples.

In regards to passing multiple models to the view you can use the ViewModel approach. You can read more about passing a model to the View here:

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

Finally, the ToDataSourceResult extension method can be called on collections and you can find more details in the API section here.

Regards,
Aleksandar
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
commented on 11 Nov 2021, 08:32 AM

Hi Aleksandar,

I think I have to simplify my question,
basically, I'm trying to pass a model with ToDataSourceResult extension method which seems impossible. Am I right?
Why I want to pass a view model instead of IQueryable or IEnumerable, is beacause I would like to operate CRUD with this only view model.

Again, thanks for your reply and detailed explanation!

ChihPei

Tsvetomir
Telerik team
commented on 15 Nov 2021, 12:39 PM

Indeed, the ToDataSourceResult() method works with collections only. The only option that you have to return a single model is actually to return a collection of a single item:

return Json(new[] { model }.ToDataSourceResult(request, ModelState));

CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
commented on 08 Dec 2021, 09:17 AM

Hi to everyone,

Thanks for the help from both Aleksandar and Tsvetomir,
I've figured it out from the reply and the official editing custom editor example,
https://demos.telerik.com/aspnet-core/grid/editing-custom
pasting the official link here, 
hope it would help somone else stucking in the same situation.

Tsvetomir
Telerik team
commented on 08 Dec 2021, 12:40 PM

Thank you for sharing the demo with our community. I was wondering if there is anything else that might need additional explanations. Is there a need for additional assistance or information? 
CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
commented on 09 Dec 2021, 07:18 AM

I think the comments above  were already quite clear,
but still thanks for asking!
Tags
Grid
Asked by
CHIHPEI
Top achievements
Rank 2
Iron
Iron
Iron
Answers by
Aleksandar
Telerik team
Share this question
or