What is the best way to invoke an Action-method method in Controller A, from Action-method method in Controller B? Is it true to have such an invocation at all?
Asked
Active
Viewed 4,316 times
2
-
In fact I have two related objects like A and B. Each one has it's separate controller. In an action from Controller A, I want to call an action in Controller B, too. – A.Dara Mar 26 '12 at 07:54
1 Answers
3
Best way is by calling return RedirectToAction()
.
But the right way to go will be to move the relevant code from your controllers/actions to an outside place, a service or business logic layer. It's wrong that they reside in your controller, it's not the place, and it now causes you headaches how to jump to another controller and then back. It's just wrong. Instead of finding a way to get around, simply fix your architecture.

Ofer Zelig
- 17,068
- 9
- 59
- 93
-
Thanks for your help. But I want to use an Action from another Controller, in the middle of an action in different controller. by RedirectToAction, I must use Return Keyword. Is there another way? – A.Dara Apr 03 '12 at 06:47
-
So don't return; put the method result in a variable and use it. For example: `var b = new ControllerB(); var action = b.DoAction();` – Ofer Zelig Apr 03 '12 at 11:56
-
1Really, I have used this, before using RedirectToAction. I declare new instance of ControllerB and then call my needed Action. Is this the best way of calling an ActionMethod of a different Controller, as I asked at first? – A.Dara Apr 03 '12 at 14:56
-
The direct answer to your question is yes. However, when using MVC architecture correctly, it isn't the correct flow. I've never had to call an action from another action, and I code in MVC since Beta 1.0 . – Ofer Zelig Apr 03 '12 at 17:36
-
1It seems that in MVC, calling an action method from another controller is not nice. In my application's architecture, I have one controller per each of my data tables. At the other hand, one Object in Business view, may has more than one table in my DB. When I define controllers per table instead of per Object, then I will have more than one controller for one Object, like now. Then, some controllers may be used in another for dosing some functions. Here is my ambiguity about calling or not calling! If I look at common controllers just as a common class, problem will be solved... – A.Dara Apr 04 '12 at 14:45
-
Dear Ofer, I asked from some friends about it, Maybe put the common controller in a ServiceController and call it from other one, is better; what do you think about it? – A.Dara Mar 02 '13 at 07:00
-
I think `RedirectToAction` doesn't work for my scenario. An action f1 in controller A, need to run action f2 in controller B, and then continue with it's code after that. can you help me please? – A.Dara Mar 03 '13 at 07:45
-
Yes, you are right. As I said in 2 previous comment, putting the common logic of the code in a common business layer or common service is good for my architecture. Can you post your comment as an answer on my question? – A.Dara Mar 03 '13 at 10:23