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

Capture Session data set by calling codebehind via Ajax

I'm building a SMS system. I've contacts and want to allow the user to select multiple contacts and send message to them. I want to maintain a user's selection of contacts temporarily so that when the user hits SEND button i can process that…
Hassan Shifaz
  • 45
  • 2
  • 7
0
votes
2 answers

Asp.Net MVC dropdownlist selected value dissappears

I have two actions and i am passing data one to another. When i pass data second action to first action dropdown's selected value disappears. I googled many hours but i failed. Here is my codes: First action which has selectlist and action…
kojirosan
  • 497
  • 10
  • 25
0
votes
2 answers

How to pass a value between controller actions

I have a page with several partial views. Each one contains a Ajax.BeginForm and all post to the same controller to perform various lookups. The ActionResult returns a value that I need to store for use in another ActionResult on the same page,…
Connie DeCinko
  • 996
  • 5
  • 19
  • 39
0
votes
0 answers

how to keep chosen value after redirect to action?

I have an opening page which lets user to enter SupervisorList page
  • Burak Karakuş
    • 1,368
    • 5
    • 20
    • 43
    0
    votes
    1 answer

    Mvc Passing data "RedirectToAction" without using Temp data

    How to pass model data while redirecting to another action without using TemptData, as given in the example. Is there an alternate way to pass EmployeeViewModel from "AddEmployee" action to "Preview" action without using TempData/ Session? public…
    m rajesh
    • 15
    • 6
    0
    votes
    0 answers

    How to store ViewData in TempData when not using Sessions?

    I recently learned that TempData for MVC is backed by Sessions by default, which is a terrible idea if one ever intends to have a web farm... I attempted to implement Brock Allen's Cookie based TempData provider, however, The ViewData object itself…
    Novox
    • 774
    • 2
    • 7
    • 24
    0
    votes
    1 answer

    When returning a view to an iframe in MVC4, the viewbag, viewdata, or tempdata doesnt pass information back to the overall view.

    I have a view that has some dropdowns that allows you to choose some surveys. When selected it loads the survey into an iframe. Loading the view into it works fine, but I am trying to display stats (summary) about this survey alongside the iframe.…
    Luke
    • 15
    • 6
    0
    votes
    0 answers

    throw tempdata from businesslogic class to controller

    i have code like this in my businesslogic class: if (userList != null) { pass = userList.Password; } else { var context = new RequestContext( new HttpContextWrapper(System.Web.HttpContext.Current), …
    Ade Zaenudin
    • 31
    • 1
    • 11
    0
    votes
    1 answer

    TempData Dependency Injection

    I'm creating a NotificationService to inject into my MVC controllers so I can display toastr messages in my views. I was initially going to store the messages in HttpContext.Current.Items but sometimes the controller redirects to another view on…
    Sam
    • 9,933
    • 12
    • 68
    • 104
    0
    votes
    0 answers

    Save HttpPostedFileBase temporary

    I have a input of type file that is sending a HttpPostedFileBase (Excel.xlxs) to my controller. This file do I need to save temporary to use in another action. This HttpPostedFileBase am I converting to DataSet using Excel Data Reader. This works…
    MrProgram
    • 5,044
    • 13
    • 58
    • 98
    0
    votes
    1 answer

    Data source not bound error in MVC 4 WebGrid upon refresh. TempData reseting

    I have an MVC 4 application that displays a list of results from a search in a WebGrid. The results page loads correctly displaying all of the data matching the search. If I reload the page I get... "A data source must be bound before this…
    Racksickle
    • 53
    • 2
    • 13
    0
    votes
    1 answer

    how to assign a value to mvc tempdata using jquery

    I know how to get MVC TempData value using JQuery, but here I am trying to assign some value to TempData using JQuery. Is this possible to assign value to MVC TempData by using JQuery? I am using MVC4 razor, by clicking on some text it should…
    sridharnetha
    • 2,104
    • 8
    • 35
    • 69
    0
    votes
    2 answers

    TempData with cookies disabled

    Is it possible to use TempData when cookies are disabled? I have a redirect check that depends on a TempData key, but when cookies are disabled, that key is always null.
    idlackage
    • 2,715
    • 8
    • 31
    • 52
    0
    votes
    2 answers

    MVC navigate back to page with same model

    I've built a Search Page with 5 properties to filter on. When a user clicks on one of the results the detail page is loaded. Now I want to provide a "Back" button so the user can go back to the Search Page with the original filter. I was thinking…
    Beejee
    • 1,836
    • 2
    • 17
    • 31
    0
    votes
    4 answers

    Passing Data from Controller to View, back to Controller, back to View

    I'm new to ASP.NET MVC. After working with traditional ASP.NET model for so long, it's taking some time for me to get to understand this model. I am going through NerdDinner to understand how things work. So, I have an object that needs to be…
    Sai
    • 693
    • 3
    • 10
    • 27