0

If I have a controller action to redirect to another action like so:

public ActionResult Index()
{
    RedirectToAction("Redirected", "Auth", new { data = "test" });
}

public ActionResult Redirected(string data = "")
{
    return View();
}

The URL bar will have something like "Redirected?data=test" in it, which AFAIK is the proper behavior. Is there a way I can pass a variable directly to the Redirected ActionResult without a change on the client?
I'd like to pass "test" directly to the Redirected ActionResult without the URL changing. I'm sure there's a simple way to do this, but it is escaping me. I know I can make a static variable outside the functions that I can pass the variable to and from, but that doesn't seem like a proper solution.

R. StackUser
  • 2,005
  • 4
  • 17
  • 24
  • Possible duplicate of [MVC - Passing Data with RedirectToAction()](https://stackoverflow.com/questions/672143/mvc-passing-data-with-redirecttoaction) – Stanfrancisco Feb 07 '18 at 16:54

3 Answers3

3

You can use TempData variable.

public ActionResult Index()
{
    TempData["AfterRedirectVar"] = "Something";
    RedirectToAction("Redirected", "Auth", new { data = "test" });
}

public ActionResult Redirected(string data = "")
{
   string tempVar = TempData["AfterRedirectVar"] as string;
   return View();
}

This link could be helpful.

cortisol
  • 335
  • 2
  • 18
  • @R.StackUser, I saw your deleted comment :) You also can use server cache. Google it "server cache ASP.NET MVC". Good luck. – cortisol Feb 07 '18 at 16:56
0

Yes, use TempData.

public ActionResult Index()
{
  TempData["data"] = "test";
  RedirectToAction("Redirected", "Auth"});
}

public ActionResult Redirected()
{
  var data = TempData["data"].ToString();
  return View();
}
Erik Philips
  • 53,428
  • 11
  • 128
  • 150
0

I hope this could help you: https://stackoverflow.com/a/11209320/7424707

In my opinion TempData isn't the most proper solution. If I were you I would look for another solution.

Otherwise, do you really need to call RedirectToAction (to call an action from another controller)? Or are your actions in the same controller for instance?

Alex
  • 729
  • 2
  • 10
  • 24
  • If a condition is met, I need to return a View generated by a different ActionResult with a specific parameter sent to that ActionResult. I was hoping cookies/TempData were not required, but I may have to use them if there isn't a better alternative. – R. StackUser Feb 07 '18 at 17:07