31

I have a controller action which I would like to call another controller action.

Is this a valid thing to do. Is it possible?

tereško
  • 58,060
  • 25
  • 98
  • 150
iasksillyquestions
  • 5,558
  • 12
  • 53
  • 75

2 Answers2

31

Controller.RedirectToAction

yfeldblum
  • 65,165
  • 12
  • 129
  • 169
  • 8
    This is issuing an HTTP 302 content redirect, involving a browser round trip. Is there a way to get rid of the round trip? – o_o Sep 16 '09 at 14:42
  • 2
    Controller actions are publicly visible HTTP endpoints. If you have two actions which need to do the same things, then what you need is either a set of nonpublic controller methods or a set of services. – yfeldblum Sep 18 '09 at 02:12
  • 3
    inside the controller that you are calling enter return View("viewName", object); inside the other controller call return (params...) – Abe Feb 16 '10 at 21:11
  • One could argue that Justice's point about Services is actually a more architecturally sound solution. – Visionary Software Solutions Oct 22 '10 at 18:14
  • 2
    Could you explain more about the services solution? – Sjoerd Sep 01 '11 at 11:10
  • 1
    why dont you just create an instance of that controller object and call the method? This is a simple OOP solution and doesnt require you to do any redirecting... –  May 08 '13 at 14:15
  • 1
    Controller actions are HTTP endpoints. They expect to be endpoints and to be called as endpoints. Calling them otherwise, for example, calling them as though they were internal service methods, can be expected in the general case not to work correctly. In various individual cases, of course, it may certainly work. But I am here giving a generic answer, not an answer to a particular case. – yfeldblum May 09 '13 at 15:33
6

As @Justice says you can use RedirectToAction. Also, you can use TempData to pass model (and other) data between controller actions.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795