liveBook · Manning

TempData

As explained in Pro ASP.NET Core 7, Tenth Edition

The TempData property exposes the controller or Razor view's temporary request-to-request data dictionary.

TempData is a key-value dictionary available through the `TempData` property on controllers and Razor views. It carries temporary data from one request to the next, making it useful when an action redirects instead of rendering its own view. Values are stored in a cookie unless session state is enabled, in which case they are stored as session data. section 22.3

A read marks a value for deletion; it does not remove the value immediately. A view can read the same key more than once during one request, but the marked value is removed after that request finishes. Do not treat TempData as durable storage or as a substitute for a view model. section 22.3 section 22.7

Show a result after a redirect

In `CubedController`, `Cube(double num)` assigns `TempData["value"] = num.ToString()` and `TempData["result"] = Math.Pow(num, 3).ToString()`, then calls `RedirectToAction(nameof(Index))`. The `Cubed.cshtml` view reads those keys to refill the input and show the result. Submit the form at `/cubed`: the result appears after the redirect, but the read values are not retained for subsequent requests. section 22.3

Listing 22.7 The contents of the CubedController.cs file in the Controllers folder
using Microsoft.AspNetCore.Mvc;

namespace WebApp.Controllers {
   public class CubedController: Controller {
       
       public IActionResult Index() {
           return View("Cubed");
       }
               
       public IActionResult Cube(double num) {
           TempData["value"] = num.ToString();
           TempData["result"] = Math.Pow(num, 3).ToString();
           return RedirectToAction(nameof(Index));
       }
   }
}

The controller listing shows exactly where values are written and where the redirect begins. section 22.3

The same request-to-request pattern works beyond a calculation:

  • A POST action can convert submitted form values to strings, put them in TempData, and redirect to a results view that reads `TempData.Keys` and `TempData[key]`. section 27.2.1
  • Razor Pages can store submitted values in a POST handler and read them directly in the redirected results page. section 27.2.2
  • A multi-page edit flow can save unfinished form data while the user creates related data, then read it on return. The Product example deserializes a JSON string from `TempData["product"]`; it does not rely on storing an arbitrary object directly. section 31.4.2

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Explains dictionary writes, view reads, storage choice, and deletion timing through a worked redirect; it does not detail storage configuration.
Examples
Uses a cube-result form, redirected form-results tables, and a related-data creation flow that preserves an unfinished edit.
What it assumes
Assumes familiarity with controller actions, redirects, and Razor views; defers detailed model binding to later treatment.
What you can do after
Apply it via ASP.NET Core TempData to pass simple or serialized values to a redirected view or page.
How it connects
Connects TempData to redirects, Razor views, Razor Pages, HTML forms, session state, and JSON serialization.

Also explained in

Compare how 2 books explain TempData →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.