0

In a controller, is it possible to return the view of an action from ANOTHER controller? The other option is to return a partial view, which uses Html.Action(...) to return the view from the other controller, but I was wondering if there's anything cleaner. Thanks.

tereško
  • 58,060
  • 25
  • 98
  • 150
Ryan Peters
  • 7,608
  • 8
  • 41
  • 57
  • 6
    The title and body of your question don't agree. Are you asking about invoking the other controller's *action* or simply a *view* that was built for another controller? – StriplingWarrior Jun 30 '11 at 14:58

5 Answers5

4

If it's just the view you want to reuse, you can pass in the path to the view. For example:

public ActionResult MyAction()
{
   // do your model magic here
   return View( "~/Views/OtherController/View.aspx", model );
}

Or you can move the view to Views/Shared like Kyle already suggested.

Marnix van Valen
  • 13,265
  • 4
  • 47
  • 74
1

Yes, if that view is a Shared view. Place the view in the Views/Shared folder in your MVC Project, then both controllers will be able to return it.

Kyle Trauberman
  • 25,414
  • 13
  • 85
  • 121
  • though this is true, if you want to load up a View that is technically designated to a different Controller, then @Marnix's answer is right. – Chase Florell Jun 30 '11 at 15:17
1

If you want to invoke an action on another controller, you can use Controller.RedirectToAction() and pass in the action and controller name.

However, this adds an additional server round trip. If you want to avoid that, you can use the TransferResult class shown here:

How to simulate Server.Transfer in ASP.NET MVC?

Community
  • 1
  • 1
Andy West
  • 12,302
  • 4
  • 34
  • 52
1

I ended up using my original solution, which was having a shared view that invokes an action. It was much less code than I needed. Thanks.

Ryan Peters
  • 7,608
  • 8
  • 41
  • 57
-1

Here's a strategy I use to invoke another action without having to create a special view just for that purpose:

Create a shared view which takes a model that defines an Action, Controller, and RouteValues, and whose sole responsibility is to call RenderAction with the values on that model.

Next create a helper method on your base controller class that takes an Action, Controller, and RouteValues as parameters, and returns the ViewResult for this shared view. That way, you can reuse this helper method and shared view on all your controllers any time you want to render some other action from another action's context.

Of course, if it's just the view and not the action that you want to invoke, Marnix's answer is correct.

StriplingWarrior
  • 151,543
  • 27
  • 246
  • 315
  • This feels a real clumsy way of going about things. Using a view to influence which controllers run feels like your hacking the MVC model. – Simon Halsey Jul 05 '11 at 16:13
  • @Simon Halsey: I suppose it might be argued that the entire idea of `RenderAction` is hacking the MVC model. Nevertheless, it helps to prevent a lot of duplicate code. Leveraging the `RenderAction` method to make it easier for one controller to call a child action on another controller can likewise help with code reuse. I appreciate the feedback, though. If you find a less clumsy way, I hope you'll share it with us. – StriplingWarrior Jul 05 '11 at 16:36