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
13
votes
1 answer

TempData Cookie Issue. The size of the request headers is too long

In Controller, I save a collection of errors into cookies via TempData var messages = new List(); ... TempData.Put("Errors", messages); TempData.Put is an extension method public static class TempDataExtensions { public static…
Muflix
  • 6,192
  • 17
  • 77
  • 153
13
votes
3 answers

Asp.net core TempData give 500 error when adding list and redirect to another view

I am try to build alerts list and added them to TempData. But it work if I did not do redirect. When I do redirect it give me 500 error. I set break point in view as well but it did not hit when did redirect other wise it hit…
Ahmar
  • 3,717
  • 2
  • 24
  • 42
11
votes
4 answers

Error 500 when using TempData in .NET Core MVC application

Hello i am trying to add an object to TempData and redirect to another controller-action. I get error message 500 when using TempData. public IActionResult Attach(long Id) { Story searchedStory=this.context.Stories.Find(Id); …
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
11
votes
1 answer

Using TempData while

I was trying to make one site as session less. So add to my web.config. After that if I execute Session["test"] = "yes"; I get an error “Object reference not set to an instance of an object.” This is fine. But with…
Jomy John
  • 6,308
  • 4
  • 28
  • 32
10
votes
6 answers

ASP.NET MVC does browser refresh make TempData useless?

If I redirect to a new page passing TempData to initialise the page it works fine, however if the user presses the refresh button in their browser the TempData is no-longer available. Given this, is there any situation where TempData could be used…
Myster
  • 17,704
  • 13
  • 64
  • 93
10
votes
1 answer

TempData becomes null after refresh the page

I used TempData to pass data from an action to another,but when i refresh the page the value of TempData becomes null, how I can solve this probleme? Thanks,
Victor
  • 235
  • 1
  • 5
  • 14
9
votes
6 answers

Temporary directory persist across program runs

I need a temporary directory, but I want full control over its creation and deletion. I will use this directory to place git repositories which I want to monitor for new commits, so I need to store them somewhere permanently. Therefore I want to…
umpirsky
  • 9,902
  • 13
  • 71
  • 96
9
votes
4 answers

TempData Not Being Cleared

I'm working on an ASP.NET MVC 3 web application, where i use TempData to store a model object, in the scenario where the user is not logged in. Here's the flow: Use submits form. Code (special action filter) adds model to TempData , redirects to…
RPM1984
  • 72,246
  • 58
  • 225
  • 350
9
votes
2 answers

ASP.NET TempData persists between requests

I am using temp data as follow in my controllers - very simple, when there is a problem: TempData("StatusMessage") = "You have no items set to Auto-Ship." Then on every page I have a user control as follows:
<% If…
Slee
  • 27,498
  • 52
  • 145
  • 243
9
votes
2 answers

access tempdata in javascript in mvc4

I am trying to access TempData in Javascript. but getting null value. I am making ajax call to Update the record and i want to display Record updated succesfully message. which will come from UpdateOperation action from the controller. but currently…
Rahul Rajput
  • 1,427
  • 3
  • 17
  • 38
8
votes
2 answers

equivalent of ASP.NET MVC TempData in ASP.NET

In ASP.NET MVC, there's a TempData which can pass data one time from one page to another. What's the equivalent for this in ASP.NET?
rob waminal
  • 18,117
  • 17
  • 50
  • 64
7
votes
1 answer

What is TempData collection used for in asp.net MVC?

What is the actual use of TempData collection in asp.net MVC, I need pros and cons of that collection, and when do I need to use it, which views it is shared upon, or any useful information about it, finally if someone can tell me when to use it…
Amr Elsehemy
  • 1,033
  • 4
  • 13
  • 24
7
votes
2 answers

Setting TempData within a ActionFilterAttribute

I have a custom action filter, that inside the OnActionExecuting, depending on certain criteria, logs out a user and redirects them to the home page of the site. The (stripped back) code for the redirect part is below …
Rob
  • 1,407
  • 2
  • 15
  • 22
6
votes
2 answers

MVC 3 tempdata container disadvantages

Although the question title appears a bit subjective I am sure there is not a lot to discuss. I am currently working on a MVC project where I am using TemData container in some areas. When I read the documentation in MSDN, it appears that using…
NiK
  • 1,827
  • 1
  • 20
  • 37
6
votes
4 answers

ASP.NET MVC Store TempData in Cookie

Is there a way to let TempData store in a browser's cookie instead of Session State. I have Session State disabled on my site. Thanks.
user342552
1
2
3
19 20