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

How pass int list from action to view, then from view to method in mvc 4?

In MVC4 application in Create(post) action I want to pass int type list to view if error occur. And then, from there to pass it to other method in same controller with ajax post. So, TempData, ViewData and ViewBag don't help me. public ActionResult…
Jeyhun Rahimov
  • 3,769
  • 6
  • 47
  • 90
0
votes
3 answers

Equivalent for ViewData from ASP.NET MVC for ASP.NET?

Is there an equivalent for the ViewData/ TempData object in ASP.NET MVC for ASP.NET? What I wanna do is to keep a Session item only alive for one request. Thanks in advance! Edit: The problem is that I have one view for updating and creating. When…
Rookian
  • 19,841
  • 28
  • 110
  • 180
0
votes
1 answer

int is null but TempData is not?

I need to pass some data between two action results so I thought I'd try TempData (I'm really new to MVC so bear with me). This is what I've tried: public ActionResult Edit(Companies companies, HttpPostedFileBase file) { if…
0
votes
1 answer

TempData issue in Chrome, while working fine in IE and Firefox

Following is the exact scenario in my application: I generate a GUID in an controller action. The GUID is stored in TempData. I pass the GUID along with a ViewModel in a razor view (MyView.cshtml) that gets opened from that controller action. A…
Nirman
  • 6,715
  • 19
  • 72
  • 139
0
votes
1 answer

Longterm usage & conflict of Session / TempData

I've an MVC3 web app which uses the default "in process" session. I've the PRG pattern in place - that is while postback if my modelstate is invalid I store the model in TempData and redirect to the original get action. In the get action I fetch the…
Hemant Tank
  • 1,724
  • 4
  • 28
  • 56
0
votes
1 answer

keeping data for multiple steps?

I have 3 steps, each step is its on view. On step 1 I ask for the start date/time(they can pick timezone and time) and store it as UTC, on step 3 I want to show the date/time based on the timezone they selected, without knowing which timezone they…
datatest
  • 483
  • 1
  • 5
  • 14
0
votes
1 answer

TempData null after return from Base Controller

TempData is populated in the Base method, but turns to null as soon as the code returns back to the derived controller's method. Derived Controller Edit Action (Post): public class ManageItemsController : BaseController { private…
crichavin
  • 4,672
  • 10
  • 50
  • 95
0
votes
3 answers

Restrict access to a page with ASP.NET

I'm developing a site with ASP.net MVC 2.0. There is a sequence of the pages which should behave like a simple wizard without return. The data should shared between pages. I want to allow redirecting to the next page of a wizard only from the…
N.Bychkoff
  • 29
  • 3
0
votes
2 answers

tempdata not working in javascript in mvc3?

i was putting my scripts in my view files (cshtml) when i started working with asp mvc3. i was doing it like this: at the bottom of each cshtml files, i put my scripts, .... and some of those scripts use TempData, everything works fine. but then…
raberana
  • 11,739
  • 18
  • 69
  • 95
0
votes
1 answer

Inconvenient to upload files to the server and use TempData

I'm trying to make a file upload to the server using Uploadify, but not working the TempData to pass variables between controllers and I have not found an error. I'm trying passing the variables fileName and file with TempData of the controller…
kalu
  • 337
  • 2
  • 9
  • 19
0
votes
2 answers

TempData[] getting wiped

I have a integer stored in TempData, and it is getting periodically wiped for an unknown reason after 30-60 seconds of browsing around my site. I have a break point on the place where the value is set and its never being set to null, yet somehow…
Dan
  • 29,100
  • 43
  • 148
  • 207
-1
votes
2 answers

TempData doesn't carry the data in second action

I have a Partial View and fill my TempData with this code block. serviceResponsecont is a list. TempData["partialResponseList"] = serviceResponseCont; After this from the PartialView I call the same controller and try to get same data like…
-1
votes
2 answers

Save variable value while navigation in MVC

I am having one page ABC.cshtml. In the page I have written code for multiple modal popup with "Back" and "Next" buttons. when I click on buttons it navigates from one modal to another. I am having "Id" which I need to persist when I navigate from…
-1
votes
1 answer

ASP MVC 5 TempData not working with RedirectToAction?

I have the following tempdata in my controller: public ActionResult Index(string query = null) { TempData["message"] = string.Format("test message"); return RedirectToAction("Index", "Posts"); } And in my…
adam78
  • 9,668
  • 24
  • 96
  • 207
-1
votes
1 answer

Pass message B/w Controllers to View Page after logout

I have to send message from controller 1 to controller 3 and finally send to the view. controller 1 public ActionResult controller1() { TempData["data"] = "work finish."; return RedirectToAction("logoff"); } Then in the controller 2 …
anand
  • 1,559
  • 5
  • 21
  • 45
1 2 3
19
20