Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect to action in other controller

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.

like image 789
Boris Callens Avatar asked Nov 24 '08 09:11

Boris Callens


2 Answers

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

like image 168
Eric Schoonover Avatar answered Sep 17 '22 01:09

Eric Schoonover


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

like image 31
Alex K Avatar answered Sep 20 '22 01:09

Alex K