-1

I have a view that executes some conditions and if one specific condition is X it must redirect to another page and show a pop-up with a message, so I was thinking in a variable with the resul of the condition that is executed id the controller (controller1.cs) and then use it in another controller (Controller2.cs) for the next actions.

Any suggests?

  • Answered here: http://stackoverflow.com/questions/15385442/passing-data-between-different-controller-action-methods – Matt Evans Mar 04 '15 at 11:04
  • return RedirectToAction("TestAction", "TestController", new {id = userId}); – SBirthare Mar 04 '15 at 11:05
  • use a TempData object which contains your variable to be available in other action. And just read from tempData in second action. if so your url will be clean. – Razack Mar 04 '15 at 11:22

2 Answers2

2

Don't use Sessions (you'll end up with using the Session as a kind of Singleton pattern with data you'll never use again). You want a RedirectToAction like this:

public ActionResult MyAction(string myResult)
        {
            if (condition)
            {
                return RedirectToAction("OtherAction", "Controller2", myResult);
            }
            else
            {
                return View();
            }
        }

You can choose to use other controllers / actions with objects you want to pass.

Leon
  • 919
  • 6
  • 21
  • Don't pass an object to a GET method, Apart from the ugly query string, it could exceed the query string character limit and throw an exception, and if the objects contains properties which are complex objects or collection binding will fail any way –  Mar 04 '15 at 11:14
  • changed it to string > wasn't about the object itself, it's an example of the routing. but thanks for the heads up – Leon Mar 04 '15 at 11:17
0

You can try using TempData -> http://www.devcurry.com/2012/05/what-is-aspnet-mvc-tempdata.html or Passing Information Between Controllers in ASP.Net-MVC -> Passing Information Between Controllers in ASP.Net-MVC.

Community
  • 1
  • 1
wpa81
  • 41
  • 4