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

ViewData not preserved in grid partial views

I have a custom grid view which uses renderRow helper similarily to Urvish post here https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/64536-How-to-use-grids-with-views-and-maybe-controls-to-make-it-look-good I'm trying to pass grid settings…
nickornotto
  • 1,946
  • 4
  • 36
  • 68
0
votes
3 answers

TempData not null after refresh

I thought TempData was supposed to become null after one refresh or page redirect. It takes two refreshes of my page to clear the data though which isn't what I want, how do I make it go null after 1 refresh/redirect? @using (Html.BeginForm()) { …
Martin Dawson
  • 7,455
  • 6
  • 49
  • 92
0
votes
1 answer

How to keep my model data when do GET>POST>REDIRECT to upload a file?

I want to create a register form using MVC which include a profile photo. I don't want to add record for people before completing the form (including profile photo upload). Also I want my UploadImage view and controller to be re-usable for many…
Ehsan88
  • 3,569
  • 5
  • 29
  • 52
0
votes
0 answers

Asp.net MVC 4 Keep TempData only on Page refresh?

I am using a dialog box to confirm an action that requires an override by a user's supervisor. When the dialog box pops open the form values are pre-populated with TempData in the view and the text boxes are locked (disabled); except for the text…
eaglei22
  • 2,589
  • 1
  • 38
  • 53
0
votes
0 answers

Cannot display message with TempData or jquery.cookie

I use Ajax for calling Action and try to display returned message from Controller to the View. However, although the message is returned with TempData, it is not displayed on the View (Actually I was displaying it in @Html.BeginForm(), but this time…
Jack
  • 1
  • 21
  • 118
  • 236
0
votes
1 answer

saving data in asp.net form temporarily

I have one requirement of saving data in asp.net form temporarily. I want to re load the saved data in form controls, if on any error or session time out. Can any body give solution for this? Thanks in advance.
0
votes
1 answer

Passing parameter from controller to view

I'm working on a C# MVC project and need to pass some data from controller to all views. My task was to save a webpage to a pdf file. there are total of 5 pages so 5 different views, I also have 5 controllers, one for each view. I created a…
Justin
  • 927
  • 11
  • 24
0
votes
0 answers

why Tempdata lost once read

I was going through some links and found something new to me, DotNetInterviewQuestion at question number 3. Does "TempData" preserve data in the next request also? "TempData" is available through out for the current request and in the subsequent…
Shekhar Pankaj
  • 9,065
  • 3
  • 28
  • 46
0
votes
1 answer

TempData not retaining value in FileContentResult

I have the following code: [Authorize] [HttpGet] public FileContentResult DownloadMsgAsPdf(string FileId, string outer = "") { var Results = (SearchResult)TempData["Results"]; var msg = Results.emails[FileId]; ... …
user2463732
  • 29
  • 2
  • 10
0
votes
2 answers

Load Balancing using SQL Server and TempData MVC

I've been told that MVC 1.0 TempData does not work under a load balancer when using SQL Server and that it is because the Dictionary itself is not serializable. We require this for a project and are looking to be able load balancer effectively. So I…
Jamie
  • 1
  • 1
0
votes
0 answers

Having some difficulties with ViewData (MVC)

I am doing an MVC5 Project for school and I'm on a model which is about Projects and Tasks. So basically, you can create, edit, update and delete Projects (CRUD), however, for each Project you can have different Tasks. So the Project and Task model…
Cajux94
  • 127
  • 1
  • 3
  • 14
0
votes
1 answer

Is TempData the best place to store a saved successfully/unsuccessfully message?

I'm implementing an ASP.NET MVC post/redirect/getpattern in an Azure website. When a user creates a new entity they are redirected from the create view to the edit view with the new object id as part of the url. The entity has quite a few fields so…
Derek Tomes
  • 3,989
  • 3
  • 27
  • 41
0
votes
1 answer

Using a NullTempDataProvider crashes at PossiblyLoadTempData()

I'm trying to use a dummy TempDataProvider for some of my controllers. The provider looks like this. public class NullTempDataProvider : ITempDataProvider { public IDictionary LoadTempData(ControllerContext controllerContext)…
Johan Alkstål
  • 5,225
  • 9
  • 36
  • 48
0
votes
1 answer

Update database record after user has logged in

What I'm trying to achieve is similar to how the login works here on StackOverflow. If you are not logged in you can still see questions but if you try to +1 some of the questions you are asked to login. When you do login your +1 is remembered and…
Høgsdal
  • 395
  • 4
  • 21
0
votes
1 answer

Unable to access TempData inside my HandleUnauthorizedRequest

I have created my own customization for the AuthorizeAttribute inside my asp.net mvc web application, and to be able to return the user to the current URL after login , i am trying to save the current URL inside a TempData and then redirect to the…
John John
  • 1
  • 72
  • 238
  • 501