Returning Multiple partial views from single Controller action?

后端 未结 3 643
心在旅途
心在旅途 2020-12-01 00:24

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.

3条回答
  •  温柔的废话
    2020-12-01 00:46

    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 ChartItems {get; set;};
       public List 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
    
    

    Blah

    @Html.RenderPartial("ChartPartialName", model.ChartItems); @Html.RenderPartial("ListPartialName", model.ListItems);

提交回复
热议问题