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

asp.net mvc - Detecting page refresh

I understand that are similar question on StackOverflow about this problem, but none of it solved my issue, so i'm creating a new one. As the title says, i'd like to detect when an user refreshs a page. I have a page where i save some log…
AdrianoRR
  • 1,131
  • 1
  • 17
  • 36
2
votes
1 answer

Fail to Use Tempdata in Filter

I'm using .NET 5.0, and I'm trying to pass Tempdata from my filter to _Layout.cshtml. Here's my fitler: using Microsoft.AspNetCore.Mvc.Filters; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; //session using…
2
votes
3 answers

ASP.NET MVC: reassign TempData

In a controller action I receive a variable from a redirect in a TempData variable public ActionResult ChangePassword() { string t = (string)TempData["myVariable"]; // works ok when coming from the redirect [..] } As I need to persist that…
pistacchio
  • 56,889
  • 107
  • 278
  • 420
2
votes
2 answers

How to add a ViewModel Object to TempData

I have the following controller with a view model as follows - [HttpPost] [ValidateAntiForgeryToken] public async Task Initiate(IFormCollection formCollection) { var studentId = formCollection["studentid"].ToString(); if…
Josh
  • 1,660
  • 5
  • 33
  • 55
2
votes
2 answers

RedirectToAction is not working while navigating from another website

I am working on an asp.net core MVC project with PayPal integration. After completing the payment, the PayPal correctly redirect to the success URL (.../PayPal/PaymentSuccess). Please see the method given below: PayPalController public class…
Vignesh VS
  • 921
  • 1
  • 14
  • 30
2
votes
1 answer

In MVC3, how do I override the TempDataProvider globally?

I'd like to change the TempDataProvider in an ASP.NET MVC3 application... I know I can do this on each controller by overriding CreateTempDataProvider... but I was wondering if there is a way to do this in 1 spot ("Global.asax?") for all…
Timothy Khouri
  • 31,315
  • 21
  • 88
  • 128
2
votes
1 answer

TempData null redirecting to View - after migrating to ASP.NET Core 3

After migrating to ASP.Net Core 3.0 my TempData is always null after redirecting to a View. Is there something wrong or missing at my startup.cs code?
Guto Barroso
  • 131
  • 1
  • 12
2
votes
2 answers

SaveTempData called even though session is disabled

I've disabled sessionState in my mvc2 app via the web.config and also created my own controllerfactory and dummy tempdata provider, as described here: How can I disable session state in ASP.NET MVC? Only I've made it so that SaveTempData throws an…
Mustafa Shabib
  • 798
  • 12
  • 35
2
votes
3 answers

TempData lost when using asp.net core 2.1 identity on localhost

Installed new asp.net core 2.1 identity (the one using RCL) to play with it. Scaffolded Login, Registration and Profile pages. Checking profile page: Areas.Identity.Pages.Account.Manage.Index.cshtml.cs I came accross this property: [TempData] …
Artur Kedzior
  • 3,994
  • 1
  • 36
  • 58
2
votes
0 answers

Cannot assign data for parameter string from tempdata asp.net core mvc

I had my code from controller here. public async Task myFunction(string myCode) { myCode = myCode == null ? TempData["thisIsTheCode"].ToString() : myCode; return View(); } public async…
Sam sam
  • 33
  • 8
2
votes
0 answers

Where to initialize TempData before any Action?

The Attach method is an ajax call made to change the variable stored inside TempData so that when the user refreshes the Index page the changes to persist. ! I tried initializing the TempData inside the constructor of the controller but the TempData…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
2
votes
2 answers

Asp.net Core TempData lifetime and search term

In my index I added a search field. When user enter a search term and click filter the index (Index) is filtered. So far so good. What I would like to achieve is that if the user performs other operations (edit, details, delete, etc) in the same…
Max
  • 6,821
  • 3
  • 43
  • 59
2
votes
1 answer

How to check null value within TempData Razor MVC that written inside js code

I'm Trying to check null value within TempData Razor MVC that written inside javascript code, but unfortunately it doesn't work. whether TempData value is null or not, the condition is always true if ('@TempData["Error"]' != null) { …
askm
  • 195
  • 1
  • 7
  • 19
2
votes
1 answer

'Tempdata' does not exist in current context

I am trying to pass some value from one controller to another something like TempData["data"]="data"; but it says Tempdata does not exist in current context.So i decided to use ViewBag and surprisingly, i get the same error message for ViewBag. I…
RelatedRhymes
  • 428
  • 6
  • 26
2
votes
0 answers

Why ViewBag or TempData cannot shown on View when using AJAX?

Before using Ajax, I used to return data with ViewBag or TempData properly. However, when using Ajax, none of the prior methods do not work even if trying to force with Javascript. Could you please clarify me where I made…
Jack
  • 1
  • 21
  • 118
  • 236