[英]ASP.NET | Call another controller's action from a controller in MVC
I am thinking about a scenario rather similar to this question asked some time ago: How to call another controller Action From a controller in Mvc我正在考虑一个与前段时间提出的问题非常相似的场景: How to call another controller Action From a controller in Mvc
In the scenario I am thinking of, controller B would however return a partial view that I would like to incorporate into the ViewResult returned by the Home controller, so the code might look like this:在我想到的场景中,controller B 会返回一个部分视图,我想将其合并到 Home controller 返回的 ViewResult 中,因此代码可能如下所示:
public class Home
{
public ActionResult Index()
{
ActionResult partialViewFromCtrl_B = RedirectToAction(actionName: "SomeAction",
controllerName: "Controller_B");
// The returned view should include the partial view `partialViewFromCtrl_B`
return View();
}
}
public class Controller_B
{
public ActionResult SomeAction()
{
// Do some stuff
return PartialView(viewName: "_SomeAction", model: someModel);
}
}
Is there a way to combine the partial view returned from controller B into the view returned from the Home controller?有没有办法把controller B返回的部分视图合并到Home controller返回的视图中? If possible, I would like to keep the logic and the views defined for controller B separate from the Home controller rather than merging everything into the Home controller.
如果可能的话,我希望将为 controller B 定义的逻辑和视图与 Home controller 分开,而不是将所有内容合并到 Home controller 中。
Your help is very much appreciated!非常感激你的帮助!
Edit: One possible solution to my scenario might by if the Index.cshtml of the Home controller looks like this编辑:如果 Home controller 的 Index.cshtml 看起来像这样,我的场景的一种可能解决方案可能是
@model SomeModelType
...
@Html.Partial(partialview: "~/Controller_B/_SomeAction", model: Model)
...
and the Index()
action of the Home controller would look like this: Home controller 的
Index()
操作将如下所示:
public ActionResult Index()
{
// Get the model from Controller_B instead of the partial view
SomeModelType someModelFromCtrl_B = new Controller_B().returnSomeModel();
return View(someModelFromCtrl_B);
}
This however does not seem to be the best approach to take here for me.然而,这对我来说似乎不是最好的方法。
So if possible I would like to combine the partial view Controller_B/_SomeAction
and the Index
view of the Home controller in the action method of the Home controller if that is possible.因此,如果可能的话,我想在 Home controller 的操作方法中组合 Home controller 的局部视图
Controller_B/_SomeAction
和Index
视图(如果可能的话)。
Through the code posted I can not understand what you want to return.通过发布的代码我无法理解你想要返回什么。 based on your little code I think you intend to return Index values in PartialView.
根据您的小代码,我认为您打算在 PartialView 中返回索引值。 Take a look at the code below:
看看下面的代码:
public class Home
{
public ActionResult Index()
{
if(//cond){
// Do some stuff
return PartialView(viewName: "_SomeAction", model: someModel);
}
else{
ActionResult partialViewFromCtrl_B = RedirectToAction(actionName: "SomeAction",
controllerName: "Controller_B");
// The returned view should include the partial view `partialViewFromCtrl_B`
return View("Index");
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.