Post from one controller action to another (not redirect)
-
05-07-2019 - |
Question
I have an action which I need to post forward onto another action if it meets certain conditions, but I am unable to preserve the form data when passing through to the next action.
The receiving action accepts a FormCollection, which the sending action
Currently, I am doing a return RedirectToAction("action","controller", form). And I can determine that the form variable has keys before it redirects (form.HasKeys() = true).
When the action is hit however, the FormCollection is empty (form.HasKeys() = false).
Any ideas? Is there a 'PostToAction' method that I am missing?
FYI: I am using ASP.NET MVC Beta. Many thanks!
Edit: For those who have this problem, please look into the PRG Pattern. This is the actual term of what I was looking to do which also enables pretty urls.
Solution
When RedirectToAction is called it will make an HTTP redirection and it will make a GET to the other action URL. If you need to reuse the code from another controller I'd do what ewalshe suggest, move the common to a BaseController or a business service and make both actions delegate to it.
This links may help:
OTHER TIPS
I would refactor the two controllers and put the common code into a helper or into a common base class. That way the actions in the two controllers can delegate to the common code.