从单个控制器操作返回多个局部视图? [英] Returning Multiple partial views from single Controller action?

查看:24
本文介绍了从单个控制器操作返回多个局部视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过 Ajax 调用更新 Multiple ,我很困惑如何从 Controller Action 方法返回这些 Multiple 视图.

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.

推荐答案

你只能从一个函数返回一个值,所以你不能从一个操作方法返回多个部分.
如果您尝试将两个模型返回到一个视图,请创建一个包含您要发送的两个模型的视图模型,并使您的视图模型成为新的 ViewModel.例如

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);
}

最后你的观点是:

@model Application.ViewModels.ChartAndListViewModel

<h2>Blah</h2>

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

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

这篇关于从单个控制器操作返回多个局部视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆