7

I want to redirect from an action in one controller to an action in a second controller. Normally I would use RedirectToAction("actionName", "controllerName", objects); The method I want to redirect to has two overloads:

  • One for HttpVerbs.Get that is used for direct linking
  • One for HttpVerbs.Post accepting reference types that get filled through modelbinding

When I do my redirect with the RedirectToAction method, I get redirected to the GET method by default which off course doesn't match my parameters.
How can I make sure it redirects to the correct action's overload?

--EDIT--
On request some more specific details:
The action I want to redirect to fills the viewData based on the parameters and then calls the correct view.

public ActionResult OverView(SearchBag searchBag, IngredientBag ingredientBag) {

It has a second version for Gets so it can work by GET too:

[AcceptVerbs(HttpVerbs.Get)]
public ActionResult OverView(int colorId, string paintCode, string name, int formulaId) {
    return OverView(new SearchBag() 
        { ColorId = colorId, PaintCode = paintCode, ColorName = name, FormulaId = formulaId }
            , formulaViewData.IngredientBag);
}

The one I'm calling now is in a different controller. It does some pre-calculations, fetches the information needed and then does the exact same thing as the previous actions. I could replicate the code from the first action, but I would rather just call that action.

[AcceptVerbs(HttpVerbs.Post)]
public RedirectToRouteResult ReCalculate(SearchBag searchBag, IngredientBag ingredientBag) {

I could create a temporary local instance of that next controller, but I noticed it doesn't have the correct HTTPContext and doesn't hit Initialization methods.

Boris Callens
  • 90,659
  • 85
  • 207
  • 305
  • You can put multiple verbs on a single action like this [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)] – Eric Schoonover Nov 24 '08 at 11:25
  • So are you trying to RedirectToAction from the ReCalculate action to the OverView action? – Eric Schoonover Nov 24 '08 at 11:26
  • yes. And since I fetched all my objects and recalculated them in ReCalculate, I don't want to have to do that again in my Overview action – Boris Callens Nov 24 '08 at 14:17
  • The point is that redirect always uses get. I don't want to use get. I already got my objects fetched and recalculated. I just want to pass them on.. – Boris Callens Nov 24 '08 at 14:17
  • you have to use get, but you can use TempData to avoid recalculating. As I recommend in my answer. – Eric Schoonover Nov 24 '08 at 17:41
  • Don't get me wrong I do appreciate your answer, but WHY does it have to use GET? I already had a request to my server and now it just has to move on over within my server itself. Everything is there in memory. Why does it have to do this detour through the extra GET? – Boris Callens Nov 25 '08 at 08:11
  • The reason it's best for it to be a GET is that if you process and return the data using the POST request if the user refreshes their browser they will get a dialog asking them if they want to re-post the data. If you redirecttoaction which causes a get that dialog will not appear on refresh. – Eric Schoonover Nov 29 '08 at 00:11

2 Answers2

3

You can not use RedirectToAction (or anything else) to cause the browser to redirect with a HTTP POST. You may be able to hack it with some JavaScript but it would be ugly.

If you can provide some more details about the target action that you would like to redirect the user to we can provide better answers for you. Please update your question with the signature of the target action and details on what you want to provide as parameter values so people can provide decent guidance.

I'm guessing what you want to do is to store some data in TempData, call RedirectToAction, load from TempData in the target Controller/Action and process.

For more information on TempData see these questions; http://www.google.com/search?q=tempdata+site%3Astackoverflow.com

Eric Schoonover
  • 47,184
  • 49
  • 157
  • 202
  • The action I want to redirect to fills my viewData with the necesary contenet (listbox items and such) and then calls the view. The second action (the one I'm calling now) first does some specific calculations and then does the exact same things as the first one. I will update my OP – Boris Callens Nov 24 '08 at 11:09
2

Since you have your object filled out, you might consider returning same View from the first action instead of redirecting.

Alex K
  • 81
  • 3