返回由单一控制器操作多个部分的看法? [英] Returning Multiple partial views from single Controller action?
本文介绍了返回由单一控制器操作多个部分的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要从一个Ajax调用更新多个,我很困惑,如何从控制器的操作方法返回多种视图。
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.
推荐答案
您只能从一个函数返回一个值,所以你不能从一个操作方法返回多个谐音。结果
如果你正在尝试两种模式回到一种观点认为,创建一个包含你要发送的模型视图模型,使您的视图模型中的新视图模型。
例如。
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屋!
查看全文