5

Let's say I have a controller:

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

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

Is there a way to accomplish this in MVC?

4 Answers 4

5

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

Source: Get ActionResult of another controller-action?

1
  • If you're trying to return a partial view result from the other controller action, this won't work because the route data will be from the original controller.
    – PJ7
    Mar 5, 2016 at 17:13
0

You can do this:

public AController : Controller
{
     public ActionResult Bar(BarViewModel vm)
     {
          FooViewModel fooVm = MakeFooVM(vm);
          var bController = new BController();
          return bController.Foo(fooVm);
     }
}
1
  • 2
    This came to mind, but I'm unsure if it's acceptable in MVC to instantiate controllers from "outside" the framework proper.
    – Dejas
    Mar 8, 2011 at 5:11
0

Have a look at this URL which explains how to pass parameters when redirecting from one action to another: http://jonkruger.com/blog/2009/04/06/aspnet-mvc-pass-parameters-when-redirecting-from-one-action-to-another/

Hope this is helpful for you.

2
  • It looks to me like using the technique you mentioned requires downloading a third party codebase. Is that correct?
    – Dejas
    Mar 8, 2011 at 6:05
  • @Dejas, you will have to make use of PassParametersDuringRedirect attribute for your controller Mar 8, 2011 at 6:09
0

An update to @WWC's answer that will help the target action be able to find the view it needs.

public AController : Controller
{
     public ActionResult Bar(BarViewModel vm)
     {
          FooViewModel fooVm = MakeFooVM(vm);
          var bController = new BController();
          var bControllerContext = new ControllerContext(this.ControllerContext.RequestContext, bController);
          // update route so action can find the (partial)view
          bControllerContext.RouteData.Values["controller"] = "B";
          bController.ControllerContext = bControllerContext;
          return bController.Foo(fooVm);
     }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.