ASP.NET Core MVC Interview Question #15
What is TempData in ASP.NET Core MVC?
MVC Fundamentals, Controllers, Views & Routing Junior Beginner
Quick Interview Answer
TempData stores data until it is read in a subsequent request, making it useful for short-lived messages across redirects. A common example is displaying a success notification after a POST action redirects to a GET action.
Detailed Explanation
TempData stores data until it is read in a subsequent request, making it useful for short-lived messages across redirects. A common example is displaying a success notification after a POST action redirects to a GET action.
The default ASP.NET Core TempData provider uses cookies, although session-based TempData can also be configured. TempData should be used for small transient values rather than large application state.
Code Example
[HttpPost]
public IActionResult Save()
{
TempData["Success"] = "Product saved successfully.";
return RedirectToAction(nameof(Index));
}