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
4
votes
2 answers

TempData persistence

I've been working with TempData lately and facing a confusing case: Supposing that the TempData is created in the following Action: public ActionResult MyAction1() { //... myTempData = TempData["myTempData"]; //.. } and is expected to be use…
Chanh Tran
  • 231
  • 2
  • 8
4
votes
1 answer

TempData persisting after read in ASP.NET MVC 2

In ASP.NET MVC 2, TempData values persist until the session ends or until they are read. In the words of Microsoft... The value of TempData persists until it is read or until the session times out. Persisting TempData in this way enables…
Mayo
  • 10,544
  • 6
  • 45
  • 90
4
votes
1 answer

Problem with TempData and Faked HttpContext using ASP.NET MVC

I am working with a faked HttpContext (code provided in the end) and probably I am missing something because I can't access TempData collection (forth line of SetFakeControllerContext method). Every time I try I get this error…
nepomucenobr
  • 278
  • 1
  • 3
  • 12
4
votes
1 answer

Popup window in .NET MVC

I have a requirement of populating a new window (With no menus, no address bar) using the existing data that I have on the page. I am using a java script function for this purpose. function popup() { …
Kanishka
  • 143
  • 1
  • 1
  • 12
4
votes
0 answers

How to get ViewBag or TempData value after ajax request and assign to c# variable in view

I have list on View from the controller, this is initialize when page is load. @{ List list = (List) TempData["onload"]; } I assign TempData["onload"] list to kendo treeview and set action("onSelect") when any item…
VKC
  • 113
  • 11
4
votes
2 answers

Passing an object array as TempData[] to view

I would like to return two values from a post action to the view in a RedirectToAction . TempData[] seems like the ideal option , as the data is only used to show a success message once the user has saved. I would like to show a small thumbnail of…
user1752532
4
votes
2 answers

TempData["sth"] as bool

This is ok: (bool)TempData["sortBool"] This is not ok: TempData["sortBool"] as bool The error states that: Error 1 The as operator must be used with a reference type or nullable type ('bool' is a non-nullable value type) …
bookmonkie
  • 439
  • 6
  • 21
4
votes
3 answers

How can I interpret HTML from TempData using MVC4?

I try to display a TempData list with HTML balise inside. Is it possible to interpret the balise and not only display it ? This is code example to illustrate what I want. Controller : List ls = new List(); ls.Add("
First div…
Alex
  • 2,927
  • 8
  • 37
  • 56
4
votes
2 answers

MVC 4 Why do i have to serialize on the server but not locally?

The webserver on my hosting company keeps complaining that the class is not marked as [Serializable]. When i run it on my localhost it works fine, no problem. As soon as i upload it to the server it asks me to serialize it? Example class: public…
Patrick
  • 5,442
  • 9
  • 53
  • 104
4
votes
1 answer

ASP.NET MVC CookieTempDataProvider.DeserializeTempData returns null

I've been trying to use CookieTempDataProvider to pass a basic message between a post (entity update) and a get (entity list) using the RedirectToAction method. When using the default TempData implementation this works fine, however when I use the…
jimr
  • 191
  • 1
  • 6
4
votes
4 answers

Persist data in MVC Razor without using TempData between requests

How do I can persist data in MVC Razor without using TempData between requests? I see when we can use TempData from this, but don't want to TempData as it creates a state on the machine. Thanks, Anish EDIT: I want to persist the previous sort…
ary
  • 597
  • 3
  • 8
  • 20
4
votes
1 answer

What is the maximum amount of data that should be placed in TempData?

Is there a cap or best practice with regards to how much data should be placed in the TempData dictionary?
Travis J
  • 81,153
  • 41
  • 202
  • 273
3
votes
1 answer

TempData lost in Edge but available in Chrome?

I am setting some TempData in the controller before redirecting: // GET public async Task SomeAction() { TempData["SomeTempData"] = "something"; return RedirectToAction("SomeAction", "SomeController"); } And then, in the View…
Stian
  • 1,522
  • 2
  • 22
  • 52
3
votes
0 answers

TempData available but not rendering in View after RedirectToAction

Edit 2: One thing I failed to mention is that I was making the post request with an onclick event in jQuery. I removed the jQuery and wrapped the button in a form. I'm guessing it has to do with that, because now it is working as I intended with…
3
votes
1 answer

Problem showing a messaje to the user using ASP MVC TempData

I'm using TempData to show a message to the user. I put a string in the TempData and later I read the string, and if it is not empty, then I show a DIV that contain the message. All works fine, and if the user refresh the page the message are not…
Jonathan
  • 11,809
  • 5
  • 57
  • 91