Value of TempData becomes null after "Redirect"

Related searches

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.

  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.

Cases where tempdata is null?, TempData is dictionary type, so you can add key-value pair data. //the following throws exception as TempData["name"] is null //because we already accessed� TempData is a kind of data storage where you put your temporary data, which will be used in the subsequent request. TempData will be cleared out after the completion of subsequent request. TempData is useful when you want to transfer non-sensitive data from one page to another page. TempData is dictionary type, so you can add key-value pair data.

Use RedirectToAction

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

TempData in ASP.NET MVC, Check for null values to avoid runtime error. TempData can be used to store only one time messages like error Tempdata is another beautiful feature in ASP. One other thing I've noticed also is that, once tempdata is null, if I keep clicking that same link after every redirect it stays at null even though the 'UpdateStatus' method is setting a value each time. If I refresh the page then click the link it will not be null anymore. It just seems so random to me.

    services.Configure<CookiePolicyOptions>(options =>
    {
                    // This lambda determines whether user consent for non-essential cookies is needed for a given request.
                    //options.CheckConsentNeeded = context => true;
                    options.MinimumSameSitePolicy = SameSiteMode.None;
    });

turn off options.CheckConsentNeeded = context => true; 

this worked for me

How to check TempData value in my view after a form post?, Tempdata null after redirecttoaction. Value of TempData becomes null after " Redirect", 4 Answers. the data in a TempDataDictionary object persists only from � The TempData in ASP.NET MVC is one of the mechanisms to pass the small amount of temporary data from a controller to a view as well as from a controller action method to another action method either within the same controller or to a different controller. TempData value will become null once the subsequent request is completed by default.

As far as i understand ViewData saves data only after redirect, not when just another Http request occur. So inside Login method(where you redirect to) this ViewData["useError"] have to be available, but Index method is just another method which is executed during another http request.That's why ViewData["useError"] is empty You can use Session if you want to store data between different Http requests

TempData null in asp.net core, Here, paste the following code in order to access controller data. @{; ViewBag. Once it sends information, it becomes null. ViewData is a TempData is mostly used to transfer value from one controller to another controller. It is most useful� Tempdata is another beautiful feature in ASP.Net MVC. We use TempData just like we use ViewData. We’ve already discussed a lot about ViewBag and ViewData stuff. TempData is a container in which we maintain the state in consecutive request. TempData is used to store the temporary data. It is using session under the hood. But it is not exactly a session. It automatically clears in application

I find .Net Core incredibly problematic. I had to turn this off in the Configuration

options.CheckConsentNeeded = context => true;

and it worked when I used Redirect to navigate to another page.

However on refreshing the page the TempDate or ViewData lose their value. But when I reassigned it to itself in the "View" it worked:

@{
TempData["somevalue"] = TempData["somevalue"];

}

Learn ViewBag, ViewData, and TempData with Example, Retaining TempData Values. If you want to access a TempData value and then retain it for a further request, you have two options. The first is the Peek method, which allows you to read a value without it being marked for deletion: var temp = TempData.Peek("FormResult"); The second option is to use the Keep method after you have accessed the

Short life means value becomes null when redirection occurs. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call. Difference between ViewBag & ViewData:

Session uses a cookie to track and identify requests from a single browser. By default, this cookie is named .AspNetCore.Session, and it uses a path of /.Because the cookie default doesn't specify a domain, it isn't made available to the client-side script on the page (because HttpOnly defaults to true).

Its value becomes null when a redirection occurs. This is one of the properties of ControllerBaseClass.ViewData returns Object so we need to typecast for complex data type and check for null values. Let's understand the concept and usage of ViewData/ViewBag/TempData practically.

Comments
  • Please post your code for controller class here...
  • Whatever I had I have posted it.
  • TempData["error"] in the (Index) function will be null because (SetError) function did not excute.
  • Hard to understand what your trying to do here. It should be return RedirectToAction("SetError); not return SetError();, but then all that method does is a redirect again, so why not just catch() { TempData["error"] = true; return RedirectToAction("Index); } in the LoginCredentials() method?
  • @user3106445 I tried doing exactly what you did and it works fine for me, the issue may be something else.
  • I am using TempData once, index is nothing but the view of the controller.
  • @user3106445 TempData:Represents a set of data that persists only from one request to the next. msdn.microsoft.com/en-us/library/… Redirec to login is the first request, redirect to index from the login is the second request
  • I have marked this answer, because now I have redirected directly to index page.
  • You should only mark an answer as accepted if its correct, and this is not!
  • So does TempData still become null even if it was never read?