Questions tagged [tempdata]

TempData is a dictionary used in C# MVC to pass data between different controllers or different actions. This feature was first introduced in MVC 1.0. To use this tag, the post must be using C#, MVC, and must be in regards to the use of TempData.

TempData uses Session internally to store its data, but unlike Session variables, TempData will clear itself.

Similarities with ViewData

TempData has some similarities with ViewData. They are both dictionaries that use strings as keys and can store primitives or objects for a value. Live ViewData, it can be used as an alternate to the model to pass data from the controller to the view.

Controller

public ActionResult TempTest()
{
    TempData["ContentString"] = "some text";
    TempData["ContentInt"] = 1;
    TempData["ContentBool"] = true;
    TempData["ContentDate"] = DateTime.Now;
    TempData["ContentObj"] = new SomeClass
    {
        SomeProp = "some text"
    };
    return View();
}

View

<div>
    TempData content string: @TempData["ContentString"]
</div>
<div>
    TempData content integer: @TempData["ContentInt"]
</div>
<div>
    TempData content boolean: @TempData["ContentBool"]
</div>
<div>
    TempData content date: @TempData["ContentDate"]
</div>
@{
    var tempDataContent = (SomeClass) TempData["ContentObj"];
}
<div>
    TempData content object: @tempDataContent.SomeProp
</div>

Result

<div>
    TempData content string: some text
</div>
<div>
    TempData content integer: 1
</div>
<div>
    TempData content boolean: True
</div>
<div>
    TempData content date: 4/16/2019 9:50:05 AM
</div>
<div>
    TempData content object: some text
</div>

Differences to ViewData

TempData holds onto its data through an entire HTTP request. This means that, unlike ViewData, the data in TempData won't blank out after a redirect.

Controller

    public ActionResult TempTest1()
    {
        TempData["Content"] = "some text";
        ViewData["Content"] = "some text";
        return RedirectToAction("TempTest");
    }

    public ActionResult TempTest()
    {
        return View();
    }

View

<div>
    TempData content: @TempData["Content"]
</div>
<div>
    ViewData content: @ViewData["Content"]      
</div>

Result

<div>
    TempData content: some text
</div>
<div>
    ViewData content:       
</div>

Once the HTTP Request is done, TempData gets blanked out, so the next HTTP request that is made will only return null for TempData.

Controller

    public ActionResult TempTest()
    {
        TempData["Content"] = "some text";
        return View();
    }

    public ActionResult TempLifeTest()
    {
        return View();
    }

View - TempTest.cshtml

    <div>
        TempData content: @TempData["Content"]
    </div>

View - TempLifeTest.cshtml

    <div>
        TempData content: @TempData["Content"]
    </div>

Result - TempTest.cshtml

    <div>
        TempData content: some text
    </div>

Result - TempLifeTest.cshtml

    <div>
        TempData content: 
    </div>

To have TempData retain it's values for the subsequent request, use TempData.Keep()

Controller

    public ActionResult TempTest()
    {
        TempData["Content"] = "some text";
        TempData.Keep();
        return View();
    }

    public ActionResult TempLifeTest()
    {
        return View();
    }

View - TempTest.cshtml

    <div>
        TempData content: @TempData["Content"]
    </div>

View - TempLifeTest.cshtml

    <div>
        TempData content: @TempData["Content"]
    </div>

Result - TempTest.cshtml

    <div>
        TempData content: some text
    </div>

Result - TempLifeTest.cshtml

    <div>
        TempData content: some text
    </div>

You can keep calling TempData.Keep() for as long as you want the data to persist for each successive request.

References

288 questions
6
votes
1 answer

ASP.NET TempData isn't cleared even after reading it

I have a controller action something similar to the below, TempData was initialized by my framework. I've noticed that TempData doesn't clear out the value once it is read as shown in the action "EmployeeUnderAge". When does TempData clears the data…
Kiran Vedula
  • 553
  • 6
  • 14
6
votes
4 answers

TempData value not persisting if used in view

I am using TempData["hdn"] = "1"; in controller If I use this @{ var hdn = (string)TempData["hdn"]; } in View, TempData["hdn"] value in getting null in POST. If I skip this code in view it persists in POST. Why this is happening?
user952072
  • 107
  • 2
  • 13
5
votes
2 answers

C# ASP.NET TempData was working but now it isn't on 2 of 3 computers

I'm working on a .net Core MVVC project devloped in VS Community 2017 and using IIS Express 10 and I'm having issues with TempData not working on two of the 3 computers I develop on. At one point it did work on all three. I use TempData to store…
HazardousGlitch
  • 150
  • 1
  • 14
5
votes
4 answers

TempData is always Null at ASP.Net Core 2.1 MVC

I would like to use TempData in my .net core mvc application. I followed the article from https://learn.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1#tempdata I always get NULL Here is my code: public async…
Si Thu
  • 1,185
  • 1
  • 13
  • 21
5
votes
1 answer

Is it possible to check TempData inside if condtion in Asp.Net MVC?

I want to check TempData inside of if condition. But I am getting an error. My Controller public ActionResult Customer(PurchaseViewModel purchaseviewmodel) { TempData["Fromdt"] = purchaseviewmodel.FromDate; TempData["todt"] =…
susan
  • 165
  • 4
  • 19
5
votes
3 answers

ASP.NET MVC TempData in browser cookie

I am trying to use a custom ITempDataProvider provider to store TempData in a browser's cookie instead of session state. However, everything works fine except that I am unable to remove the cookie from the Response stream after reading it. Any…
user342552
5
votes
1 answer

TempData is empty after RedirectToAction in catch block. What could be the reason?

I've been using TempData for a long time and faced strange issue for me. I have basic scenario: [HttpPost] public ActionResult Create(ProductCreateModel newProduct) { // create and save product to db // try upload product to external site …
Dmytro
  • 16,668
  • 27
  • 80
  • 130
5
votes
4 answers

TempData Wrapper

Because I use same keys for TempData over and over again, I would like to make it easier to keep track of these keys. It would be great if eventually I could write something like this: MyTempData.Message = "my message"; instead…
Dmitry Efimenko
  • 10,973
  • 7
  • 62
  • 79
4
votes
2 answers

ASP.NET MVC: How to handle cross-action TempData and ViewData

I'm trying to find a good way to handle the following scenario (I'm still kinda new to this): A user can register through my site using an RPX/OpenId provider. Step 1: The user authenticates through a provider. The provider returns a temporary token…
Denny Ferrassoli
  • 1,763
  • 3
  • 21
  • 40
4
votes
2 answers

TempData works, but is delayed by one request

I'm using TempData to carry messages with Redirect after Post. The controller sets the tempdata as shown here: TempData["message"]="foo"; return RedirectToAction("Index"); In the _Layout.cshtml, I have the following fragment: @{var temp =…
MZywitza
  • 3,121
  • 1
  • 17
  • 12
4
votes
2 answers

why TempData[] doesnt work with IE

İn my MVC3 project, there is plenty of TempData[] that I am using for passing datas between actions. And it works totaly perfect when I use Chrome. But in IE I can't get values of TempData[] items. if anyone knows whats the problem and how can I…
serhads
  • 462
  • 2
  • 7
  • 22
4
votes
1 answer

How to use Tempdata to display the list

I have did the excel upload in dotnet core .I had to use tempdata to retrieve the details of the excel in list.Instead in my below code i had used Static object to retrieve the list.My code works as like this ,when i click on upload button it will…
anjalin sneha
  • 61
  • 1
  • 7
4
votes
1 answer

TempData not working when published to Azure

I have a site I'm playing with to get the hang of Razor Pages. I have a weird situation I'm unsure what is happening or how to resolve. I'm using [TempData] to pass a message on redirect. The app works perfectly locally. Once published to Azure I…
Tim
  • 563
  • 2
  • 6
  • 20
4
votes
1 answer

Null TempData when passing data from controller to view MVC

I have the following class in a Controller passing data to a View: public ActionResult ControllerToView(){ ... TempData["example"] = "this is a message!"; ... return Redirect("http://myViewPageLink"); } In my View, I am trying to…
Nicole
  • 107
  • 1
  • 2
  • 12
4
votes
3 answers

Access TempData within custom middleware

I have custom middleware that provides global error handling. If an exception is caught it should log the details with a reference number. I then want to redirect the user to an error page and only show the reference number. My research shows that…
1 2
3
19 20