Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning Multiple partial views from single Controller action?

I need to update Multiple from an Ajax call , I am confused as in how to return these Multiple views from the Controller Action method.

like image 673
Nikshep Avatar asked Aug 04 '11 06:08

Nikshep


People also ask

How do I return multiple partial views from controller action?

You can only return one value from a function so you can't return multiple partials from one action method. If you are trying to return two models to one view, create a view model that contains both of the models that you want to send, and make your view's model the new ViewModel.

How do you return a partial view from controller?

In ASP.NET Core MVC, a controller's ViewResult is capable of returning either a view or a partial view. In Razor Pages, a PageModel can return a partial view represented as a PartialViewResult object. Referencing and rendering partial views is described in the Reference a partial view section.

Can we have multiple views for single controller?

Yes You can use multiple View in one Controller.

Can we return partial view from action method?

To return a Partial view from the controller action method, we can write return type as PartialViewResult and return using PartialView method.


1 Answers

You can only return one value from a function so you can't return multiple partials from one action method.
If you are trying to return two models to one view, create a view model that contains both of the models that you want to send, and make your view's model the new ViewModel. E.g.

Your view model would look like:

public class ChartAndListViewModel  {    public List<ChartItem> ChartItems {get; set;};    public List<ListItem> ListItems {get; set;}; } 

Then your controller action would be:

public ActionResult ChartList()  {    var model = new ChartAndListViewModel();    model.ChartItems = _db.getChartItems();    model.ListItems = _db.getListItems();     return View(model); } 

And finally your view would be:

@model Application.ViewModels.ChartAndListViewModel  <h2>Blah</h2>  @Html.RenderPartial("ChartPartialName", model.ChartItems);  @Html.RenderPartial("ListPartialName", model.ListItems); 
like image 199
dnatoli Avatar answered Sep 23 '22 15:09

dnatoli