-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?

3
  • Answered here: stackoverflow.com/questions/15385442/… Commented Mar 4, 2015 at 11:04
  • return RedirectToAction("TestAction", "TestController", new {id = userId}); Commented Mar 4, 2015 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. Commented Mar 4, 2015 at 11:22

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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
changed it to string > wasn't about the object itself, it's an example of the routing. but thanks for the heads up
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.