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

Store data and retrieve after redirect to third party URL

I have some data that I need to get after redirecting my URL to yahoo auth for authentication and access token. I tried using Session and tempData, but both get cleared after redirection and callback to another ActionMethod. Tried using HttpCookie…
0
votes
0 answers

'Newtonsoft.Json.JsonSerializationException' occurred in Newtonsoft.Json.dll but was not handled in user code

I need to store my IEnumerable of objects into Tempdata for my next request from the server. Since Tempdata cannot store Objects I found the below extenstion method to serialize and deserialize before putting into Tempdata. After this method, I am…
0
votes
2 answers

TempData Dictionary is null after Redirect to page

So I have this issue that I am not unable to solve the way I think it's supposed to be solved. I have an ASP.NET Core 2.1 Razor pages project. The code is pasted below and my problem is the following: On the index page, I have a search form. The…
user6106197
0
votes
1 answer

MVC Net Core Can TempData store Enumerables?

Running this Index controller gives me errors, when I try to pass IEnumerable into Tempdata. When debugging, TempData["ProductCategory"] seems to store expected data in Watch, but cannot render Html View after storing. However, the Html does show…
user10432265
0
votes
1 answer

Access Controller Context/ TempData from business objects

I am trying to build a session/tempdata provider that can be swapped. The default provider will work on top of asp.net mvc and it needed to access the .net mvc TempData from the business object class. I know the tempdata is available through the…
thanikkal
  • 3,326
  • 3
  • 27
  • 45
0
votes
1 answer

Error in executing tempdata

Form.php(controller) public function dispdata() { $result['data']=$this->Form_model->displayrecords(); $this->load->view('display_records',$result); if (!$this->session->userdata('ci_session')) { …
0
votes
1 answer

TempData currently unable to handle this request

How can I fix TempData is not working, page redirects to localhost is currently unable to handle this request. HTTP ERROR 500 What I want to achieve is to create a form that accepts model, and can add multiple data, I want to store the temporary…
0
votes
1 answer

How to reset TempData in a specific condition but can use it in one method

I have multiple Checkboxes in a view and on true or false i am querying with database to get the records of selected Category and than export those records in excel. I have multiple ActionMethods in my project and I am using Tempdata to hold the…
Faizan
  • 542
  • 5
  • 16
0
votes
0 answers

Why script has stopped working when I started using ajax call?

I have used tempData to hold a message returned from database to controller and then using it in view. It worked until I made an ajax call to post data. [HttpPost] public ActionResult AddAppointment(AddBookingsViewModel AddBookingVM) { …
user9575239
0
votes
3 answers

Tempdata becomes null after redirect to another page

When the user successfully login, i'm storing the username in tempdata so i can use it in my _Layout: TempData["username"] = model.Email.Split('@')[0]; TempData.Keep("username"); on my _Layout:
0
votes
1 answer

Getting custom type from TempData asp.net mvc 2

I have a custom class MyCustomType. In that class I have a property MyCustomProperty of type bool and another property MyCustomProperty1 of type bool also. I need to check if MyCustomProperty is true in my View. I`m doing the following thing: <%if (…
Yaroslav Yakovlev
  • 6,303
  • 6
  • 39
  • 59
0
votes
1 answer

How to fix "Cookie not Sent Over SSL (4720)" ASP.NET MVC?

1.The Tester test my ASP.NET MVC website and report "Cookie not Sent Over SSL(4720)" issues. 2.And they provide me to solve this issue by Add in Web.config and then I followed the…
user2955394
  • 1,063
  • 4
  • 17
  • 34
0
votes
0 answers

Convert TempData to Int

I have a problem to convert TempData["Id"] to int. I assign an int value to the TempData["Id"], and want to use it as int. Here is my code: TempData["Id"] = 1; int Id= (int)TempData["Id"]; But it didn't work. The Id is null. I try to use some…
yusry
  • 148
  • 2
  • 15
0
votes
2 answers

Asp.Net mvc 5 tempdata doesn't work

I am learning ASP.net MVC5 with the code in book. public ActionResult DemoTempData() { ViewData["Msg1"] = "From ViewData Message."; ViewBag.Msg2 = "From ViewBag Message."; TempData["Msg3"] = "From TempData Message."; return…
Echo
  • 1
  • 2
0
votes
1 answer

Keeping data between ActionResults

I am populating a list based on data returned from a stored procedure, this first occurs in the SpecificArea ActionResult: public ActionResult SpecificArea(ModelCallDetails call, int id = 0 ) { ReturnSpecificAreas(call, id); …
elszeus
  • 131
  • 1
  • 2
  • 13