Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value of TempData becomes null after "Redirect"

Tags:

c#

asp.net-mvc

I am facing issues with TempData after Redirect.

public ActionResult LoginCredentials()
{
    // Calling "SetError()" in catch(), if password mismatch.                        
    try{}

    catch()
    {
      return SetError();
    }   
}

public ActionResult SetError()
{
    // set the value of TempData as "true"                        
    TempData["error"] = true;
    return Redirect("/Login");                
}


public ActionResult Index()
{
    ViewData["useError"]= TempData["error"]; // at this point TempData["error"] is null.
    ...
}

In SetError() value of TempData is successfully set as true, issue takes place after "Redirect", value becomes "null" and I can't use it anymore.

like image 612
user3106445 Avatar asked Jul 27 '15 11:07

user3106445


1 Answers

  1. maybe the browser is cookieless
  2. the data in a TempDataDictionary object persists only from one request to the next, unless you mark one or more keys for retention by using the Keep method, accoding to your code, if you redirect to login page, and then redirect to index, the value will be null. you can only read it at login page.
like image 61
aspark Avatar answered Oct 21 '22 22:10

aspark