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
97
votes
8 answers

ASP.NET MVC - TempData - Good or bad practice

I'm using the AcceptVerbs method detailed in Scott Gu's Preview 5 blog post for dealing with form entries in ASP.NET MVC: User gets an empty form via GET User posts the filled in form via POST to the same Action The Action validates data, takes…
anonymous
  • 6,825
  • 8
  • 47
  • 60
89
votes
5 answers

TempData keep() vs peek()

What is the difference between keep() and peek()? MSDN says: keep(): marks the specified key in the dictionary for retention. peek(): returns an object that contains the element that is associated with the specified key, without marking the key…
Ghasan غسان
  • 5,577
  • 4
  • 33
  • 44
84
votes
3 answers

Store complex object in TempData

I've been trying to pass data to an action after a redirect by using TempData like so: if (!ModelState.IsValid) { TempData["ErrorMessages"] = ModelState; return RedirectToAction("Product", "ProductDetails", new { code = model.ProductCode…
elolos
  • 4,310
  • 2
  • 28
  • 40
79
votes
3 answers

Using Tempdata in ASP.NET MVC - Best practice

I am using ASP.NET MVC 3 to build a web application. What I am trying to do is pass values between two controllers, though there are many ways to do this I am particular interested in using TempData for this. public ActionResult Action1() { …
Yasser Shaikh
  • 46,934
  • 46
  • 204
  • 281
77
votes
5 answers

How can I disable session state in ASP.NET MVC?

I would like to have a very lightweight ASP.NET MVC site which includes removing as many of the usual HttpModules as possible and disabling session state. However when I try to do this, I get the following error: The SessionStateTempDataProvider…
Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
68
votes
7 answers

When to use TempData vs Session in ASP.Net MVC

I am trying to get the hang of MVC framework so bear with me. Right now, the only thing I'm using the session store for is storing the current logged in user. My website is simple. For this example, consider three domain objects, Person, Meeting,…
scottm
  • 27,829
  • 22
  • 107
  • 159
33
votes
3 answers

Asp.Net core Tempdata and redirecttoaction not working

I have a method in my basecontroller class that adds data to tempdata to display pop-up messages. protected void AddPopupMessage(SeverityLevels severityLevel, string title, string message) { var newPopupMessage = new PopupMessage() { …
BrilBroeder
  • 1,409
  • 3
  • 18
  • 37
31
votes
1 answer

Is it possible to access the TempData key/value from HttpContext?

I'm trying to crate a custom action filter attribute. And some where, I need facilities, such us TempData[key] and TryUpdateModel... My custom attribute class deriving from the ActionFilterAttribute, I can access both below methods. public override…
Richard77
  • 20,343
  • 46
  • 150
  • 252
23
votes
2 answers

Where does TempData get stored?

Where does TempData get stored in the ASP.NET MVC Framework (more specifically, ASP.NET MVC 2)? Is it stored at server-side, or is sent to the client?
Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
22
votes
5 answers

TempData: Is It Safe?

I am using the TempData in order to preserve my model in when using a RedirectToAction. It works fine, but I have a nagging feeling that it might not be the right thing to do. I really try to avoid using Session data, and I've read that TempData…
ek_ny
  • 10,153
  • 6
  • 47
  • 60
20
votes
3 answers

MVC C# TempData

Can somebody please explain the purpose of TempData in MVC. I understand it behaves like ViewBag but what does it do beyond that.
Nate Pet
  • 44,246
  • 124
  • 269
  • 414
19
votes
1 answer

ASP.NET MVC exception handling

Is it OK to catch my exceptions in the controller's actions? Is there any better way of doing it? I'm actually catching my exceptions in the controller and using TempData to show a message to the user, but I have a weird feeling about this approach.…
Carles Company
  • 7,118
  • 5
  • 49
  • 75
17
votes
3 answers

How to check TempData value in my view after a form post?

I fill my TempData from a FormCollection and then I try to check the value of my TempData in my view with MVC 4 but my if statement isn't working as I expect. Here is my code. Controller : [HttpPost] public ActionResult TestForm(FormCollection data)…
Alex
  • 2,927
  • 8
  • 37
  • 56
16
votes
4 answers

Is there a possible "race condition" when using Asp.Net MVC TempData across a redirect?

When using TempData, my understanding is that it will keep whatever you put in it around for only one request. So when using TempData to retain data across a redirect (in order to use the Post-Request-Get pattern), isn't it possible that some other…
Dan P
  • 815
  • 1
  • 9
  • 11
14
votes
1 answer

TempData won't destroy after second request

I'm putting a value into TempData on first request in an actionfilter. filterContext.Controller.TempData["value"] = true; after that a second request comes in and I check for the value filterContext.Controller.TempData.ContainsKey("value") the…
user49126
  • 1,825
  • 7
  • 30
  • 53
1
2 3
19 20