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
3
votes
3 answers

Copy ModelState Errors to TempData & Display them In the view

Most of my action methods return PartialViews on success and RedirectToAction results on failure. For that, I would like to copy the model state errors into TempData so I could display them to the user. I've read several questions here on SO and…
Kassem
  • 8,116
  • 17
  • 75
  • 116
3
votes
1 answer

ASP.NET MVC 3 Custom Action Filter - How to add incoming model to TempData?

I'm trying to build a custom action filter which grabs the incoming model out of the filter context, adds it to tempdata, then does "other stuff". My action method looks like this: [HttpPost] [MyCustomAttribute] public ActionResult…
RPM1984
  • 72,246
  • 58
  • 225
  • 350
3
votes
3 answers

TempData are always empty

I want to use TempData to store messages between Post and followed redirect but TempData are always empty. I have BaseContoller offering some infrastructure for passing TempData. Simplified code looks like: public abstract class BaseController :…
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
3
votes
7 answers

PHP: Alternative to SESSIONS

I have a PHP application that relies on session variables quite a lot. After login the user get redirected to a page that executes code to set up a load of session variables depending on who the user is. The application is using data from different…
iamjonesy
  • 24,732
  • 40
  • 139
  • 206
3
votes
2 answers

Dynamic TempData in ASP.NET MVC 3

I'vm been trying to get a site running using ASP.NET MVC 3 and I came across the new dynamic ViewModel. It's great to pass values quickly to the view without using "magic strings". I'm wondering if there's something similar for the TempData that…
Carles Company
  • 7,118
  • 5
  • 49
  • 75
3
votes
3 answers

ASP.Net MVC TempData - how to keep state

We are using ASP.Net MVC TempData to store form data between page refreshes. We have a button on the page that allows the user to perform a certain action. If the user clicks this button one time, it works fine. If they click the button twice,…
alchemical
  • 13,559
  • 23
  • 83
  • 110
3
votes
5 answers

TempData["message"] isn't reliable-- what am I doing wrong?

I'm using TempDate["Message"] to show little update banners as the user does things on my site like this: [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Admins")] public ActionResult Delete(int id) { _Repo.DeletePage(id); // soft-delete …
Michael Haren
  • 105,752
  • 40
  • 168
  • 205
3
votes
1 answer

TempData only displaying in child partial view, not parent partial view

I am creating an MVC mini app within an Umbraco web page. I've asked questions in the Umbraco user forums but so far have not gotten a solution. My Umbraco 6.1.6 site has a webForms MasterPage(s). On my content page is a rich text editor in which…
Connie DeCinko
  • 191
  • 1
  • 13
3
votes
1 answer

Casting collection of objects from TempData (MVC C#)

I am trying to put my collection of objects into TempData like this: [HttpPost] public ActionResult PandoraRemovalTotal2(List model, string hdnMem) { …
nick gowdy
  • 6,191
  • 25
  • 88
  • 157
3
votes
2 answers

asp.net mvc and web farm

given the nature of the project, I need to store a simple object (with 3/4 properties) in TempData. It is a read once write once so that's fine but does need to be passed between a few core methods/actions. question is: How can I make it work with…
Ahmed ilyas
  • 5,722
  • 8
  • 44
  • 72
3
votes
3 answers

Tell Actions/Controllers to Persist TempData

I understand that TempData is designed to work only between a single page request. But I think have a situation I think breaks the intended functionality. I use Controllers in preference to Handlers for delivering images. I don't now if that is best…
kim3er
  • 6,306
  • 4
  • 41
  • 69
3
votes
1 answer

Pass information back to the view when using Redirect()

I am calling a controller from more than one page and am using a returnUrl parameter to return the correct calling location: public ActionResult EmailOrder(int id, string returnUrl) { var message = "The order has been emailed"; if…
Sean Kearon
  • 10,987
  • 13
  • 77
  • 93
3
votes
2 answers

Should I be passing values through RedirectToAction or TempData?

I've seen some articles (even MSDN) suggest TempData for passing data between ActionMethods. But I've seen others here say that TempData should be avoided. What's the best practices way to approach this? Here's some code to show my situation. Note:…
dotnetN00b
  • 5,021
  • 13
  • 62
  • 95
2
votes
1 answer

Ajax.BeginForm TempData does not output after post

I haven't found another question that answers this directly. I have an Ajax.BeginForm on my View like so @using (Ajax.BeginForm("Action", "Controller", new AjaxOptions())){ //Stuff
@TempData["Key"]
} In the action…
quitstalin
  • 163
  • 1
  • 2
  • 18
2
votes
5 answers

ASP.NET MVC : Preserve TempData across multiple requests

There are values I need to pass when I perform redirects. I want to use TempData to accomplish this, but have encountered an issue. I use a special controller to generate dynamic JavaScripts. For example, there might be a script tag like…
user113003
  • 21
  • 1
  • 2