有没有办法返回从控制器B的一个ActionResult,同时提供一个特定的模式向B从控制器A叫什么名字? [英] Is there a way to return an ActionResult from Controller B called from Controller A while providing a specific model to B?

查看:92
本文介绍了有没有办法返回从控制器B的一个ActionResult,同时提供一个特定的模式向B从控制器A叫什么名字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个控制器:

Let's say I have a controller:

public BController : Controller
{
    public ActionResult Foo(FooViewModel vm)
    {
       ...
    }
 }

和在同一时间,我在执行另一个控制器AController,我想渲染BController.Foo的传递特定的模型对象的结果的行动。所以:

and at the same time I'm implementing an action in another controller AController where I want to render the result of BController.Foo passing a specific model object. So:

public AController : Controller
{
     public ActionResult Bar(BarViewModel vm)
     {
          FooViewModel fooVm = MakeFooVM(vm);
          return ... ; // pass fooVm to BController
     }
}

有没有办法在MVC中做到这一点?

Is there a way to accomplish this in MVC?

推荐答案

缺少在上面的回答的一个步骤。创建控制器后,您需要设置ControllerContext使控制器的请求,响应和HttpContext的将被填充。刚刚创建的控制器将导致控制器的上下文设置为空值。

Missing a step in the answer above. After you create the controller, you need to set the ControllerContext so that the controller's Request, Response, and HttpContext will be populated. Just creating the controller will result in null values for the controller's context settings.

public AController : Controller
{
     public ActionResult Bar(BarViewModel vm)
     {
          FooViewModel fooVm = MakeFooVM(vm);
          var bController = new BController();
          bController.ControllerContext = new ControllerContext(this.ControllerContext.RequestContext, bController);
          return bController.Foo(fooVm);
     }
}

来源:拿到另一个控制器动作的ActionResult

这篇关于有没有办法返回从控制器B的一个ActionResult,同时提供一个特定的模式向B从控制器A叫什么名字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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