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

What to use as the key in a custom TempData provider?

I'm investigating storing TempData in a better place. There is a walkthrough for how to do that with MongoDB but the key used for storage (item.SessionIdentifier == controllerContext.HttpContext.Request.UserHostAddress, the IP address) is clearly…
boot4life
  • 4,966
  • 7
  • 25
  • 47
0
votes
1 answer

Can't seem to pass a model to an ActionAsPdf

I need to generate a pdf copy of each partner's AR and for that I'm trying to pass a model to another view via TempData but it always returns null. Here's my code below. public ActionResult GenerateARPDFs(string dateasof) { dateasof =…
beejm
  • 2,381
  • 1
  • 10
  • 19
0
votes
2 answers

Tempdata not being cleared when i click back arrow from another action?

I want show the alert message when the user is added. It happens smoothly but when i press the back arrow of the browser from the another action it still shows the alert message. //this is my partial view
Anand Shrestha
  • 39
  • 1
  • 11
0
votes
1 answer

How could I handle an asp.net mvc error AND use the same view from the controller?

I have actions like this: [AcceptVerbs(HttpVerbs.Post)] public ActionResult New(Product product) { try { if(ModelState.IsValid) { _productService.Create(product); TempData["success"] = "Product created…
Victor Rodrigues
  • 11,353
  • 23
  • 75
  • 107
0
votes
2 answers

uable to assign TempData value to a variable in a View in mvc 4

Basically I am learning about difference between ViewBag, ViewData, TempData in practical.I am in confusion with the theory of these concepts so I want to see how things happen in practical. I learned that TempData is used to send data from…
Intriguing
  • 125
  • 3
  • 13
0
votes
2 answers

Why doesn't ASP.NET TempData work when modifying Web.config?

I have a function in my Global.asax.cs file that applies a couple changes to the Web.config file, if needed. The file is saved with: config.Save(ConfigurationSaveMode.Modified); I recently discovered that TempData wasn't working for my app: it…
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
0
votes
0 answers

pass a list value from one controller to another controller action in vb.net

I wanted to know how to pass a list(of string) values from one controller to another controller action. I have read that we can use TempData etc, but it is not working for me. Initially I passed like this Return RedirectToAction("Summary", New With…
Siddharth
  • 436
  • 2
  • 11
  • 29
0
votes
1 answer

TempData gets overrided when I try to ReturnRedirect

I want to redirect to a previous page after I submit some data in database. I am using a TempData to retrieve ID on current Page, then I am going on the page with the form I want to submit (which have an ID), but after the POST method is fired…
Eduard
  • 732
  • 1
  • 7
  • 20
0
votes
1 answer

What's the best way to have data available when a user is logged in?

In my application I have the requirement to store, for the period the user stay logged in, some variables that's used to provide a customized experienced on how the user views it's data (pre-defined filters, language, etc.). My needed data is not…
Lorenzo
  • 29,081
  • 49
  • 125
  • 222
0
votes
2 answers

How to pass guid using TempData

I am using TempData to pass the guid between two action methods within the same controller, but I am getting null in the second method where I am calling it.I am not able to figure out why I am getting a null value. Here is what I could do so…
Sumedha Vangury
  • 643
  • 2
  • 17
  • 43
0
votes
0 answers

MVC pass model between Parent and Child Window

Thanks in advance. Please excuse me for my grammer. I tried my best to explain my issue In my quest of solving below question I started to develop a POC first. C# MVC No submit pass object between views I am having an issue using TempData object…
Ziggler
  • 3,361
  • 3
  • 43
  • 61
0
votes
4 answers

How to pass data from different controllers

I am trying to pass a value which is stored in one controller to another, code is below: Charities Controller [HttpPost] [ValidateAntiForgeryToken] public ActionResult Donate([Bind(Include = "ID,DisplayName,Date,Amount,Comment")]…
John
  • 117
  • 1
  • 2
  • 13
0
votes
1 answer

Passing form values from one controller view to another form on a different controllers view in asp MVC

Okay I'm new to MVC and trying to make me a webpage where I can transfer to small form box with a zip code and button, to quote page that will fill in the zip code part on the quote page. My problem is I have two controllers a homeController that…
user5889796
0
votes
3 answers

Loop foreach over List of objects (cannot operate on variables of type 'object')

I am trying to save some objects into a list for later use. Here is how I store them: List engagementsList = new List(); foreach (Engagement engagement in db.Engagements) { // Implementation here } TempData["DeletedE"]…
James
  • 1,471
  • 3
  • 23
  • 52
0
votes
1 answer

ASP.NET Session State and TempData

I've inherited a site which uses TempData to pass various model state objects back to contoller methods. i.e. a failed login will have the error message stored in the TempData object and read back out of memory when the index method gets called.…
P456678
  • 175
  • 1
  • 2
  • 10