Sunday, March 20, 2011

Post from one controller action to another (not redirect)

Hey all!

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!

From stackoverflow
  • 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.

  • 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:

    Dan Atkinson : Eduardo, thankyou for the linkage! It helped looking at Stephen Walther's example.

0 comments:

Post a Comment