简体   繁体   English

ASP.NET | 从 MVC 中的 controller 调用另一个控制器的操作

[英]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/_SomeActionIndex视图(如果可能的话)。

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.

相关问题 ASP.NET MVC 4从另一个控制器调用一个控制器方法 - ASP.NET MVC 4 call a controller method from another controller 从.net中的另一个应用程序调用asp mvc Controller Action? - Call asp mvc Controller Action from another application in .net? 调用另一个控制器,并对调用的控制器操作MVC ASP.NET使用不同的布局 - Call another controller and use a different Layout for the called controller action MVC ASP.NET 将数据从一个控制器动作传递到ASP.Net MVC 5中的另一个控制器动作 - pass data from one controller action to another controller action in ASP.Net MVC 5 ASP.NET MVC从同一控制器发布到控制器操作 - ASP.NET MVC post to controller action from same controller 将变量从视图传递到 controller 操作方法,然后传递到同一 ASP.NET MVC controller 中的另一个方法 - Pass a variable from a view to a controller action method and then to another method in the same ASP.NET MVC controller 从不同的控制器直接调用 ASP.NET 控制器操作 - Directly call ASP.NET controller action from a different controller ASP.NET MVC AJAX 调用应用程序/控制器/操作? - ASP.NET MVC AJAX call Application/Controller/Action? ASP.NET MVC以编程方式初始化Controller并调用操作 - ASP.NET MVC initialize Controller and call action programatically 如何从 ASP.NET Core MVC 中的一个控制器调用或返回另一个控制器的视图 - How to call or return the view of another controller from one controller in ASP.NET Core MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM