Updated April 10, 2023
Introduction to TempData in MVC
Temp data is nothing but can be used to store the data that is temporary data. This temporary data will be used in a subsequent request. This request will be automatically get cleared off after the request has been completed. It is mostly useful in the case where we want to pass the non-sensitive data from one action method another. It is derived from the TempDataDictionary, which is a dictionary type. It uses the session internally to store the data. It is a dictionary object and used for passing the data from controller to controller or the controller to view. So just like View Data, we can use Tempdata.
Key Points on TempData
- It is used to store the data between two sequential requests.
- During redirection, the values of the TempData will be retained.
- It is like a session, which is short-lived, and uses a session internally to store the data.
- The value of Tempdata must be type cast already whenever you are using it.
- To avoid run time error always, check for the null values.
- Temp data is mostly used in messages like error messages or validation messages which will store a time message.
- To provide all the tempdata values in a third request we can use Call TempData.Keep() .
How to Implement TempData in MVC?
In the ASP.NET MVC framework, we have access to the session object, which is derived from the HttpSessionBase. HttpContext class provides us the session property in MVC. However, MVC has its own object that is tempdata object. This object will be used to pass the data amongst the controllers. This tempdata is very short in time span and derived from TempDataDictionary. when the target value is fully loaded it sets its value to null.
Examples of TempData in MVC
Let us discuss about tempdata with the help of example so that it will more clearly.
Example #1
Let us take an example where a value is set in the tempdata object, which is a string value, in a controller. This will be redirected to another controller and will be displayed in a view finally.
First Controller
public clas:s FirstController : Controller
{
// GET: First
public ActionResult Index()
{
TempData["Message"] = "Welcome to EDUCBA learning!";
return new RedirectResult(@"~\Second\");
}
}
Second Controller:
public class SecondController : Controller
{
// GET: Second
public ActionResult Index()
{
return View();
}
}
View of Second Controller
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
<div>
@TempData["Message"];
</div>
</body>
</html>
Output:
Example #2
Let us take another example, now we will be listing out all the available learnings in the EDUCBA. Here we are using this tempdata to ass the data from one action to another action. Below is the code for controller.cs.
TempDataController.cs
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace ViewBagExample.Controllers
{
public class ViewBagController : Controller
{
// GET: ViewBag
public ActionResult Index()
{
List<string> Learning = new List<string>();
Learning .Add("Data Science");
Learning .Add("Excel");
Learning .Add("Marketing");
Learning .Add("Project management");
Learning .Add("Design");
Learning .Add("Human Resource");
TempData["Learning "] = Learning ;
return View();
}
}
}
Code for View:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<h2>List of available Learnings</h2>
<ul>
@{
foreach (var Learning in TempData["Learning "] as List<string>)
{
<li> @Learning </li>
}
}
</ul>
</body>
</html>
We will be getting below Output after executing this above code. We will be getting the list of available learning as shown below in the snippet.
Output:
Where TempData is Stored?
We must have thinking where exactly this tempdata is stored and how it proceeds. So, the temp data is stored in the session and this is by default case. Mostly we are not bothered that from where it comes and how it gets stored as long as we are getting what we want on time.
However, suppose we now want to switch from default state mode to state server mode or say SQL server mode.so these other entire modes will require the objects to be in the serialized way. In addition, if it is not in a serialized manner then we will be getting some session errors.
How Long does TempData Last in MVC?
Usually, a value of temp data in ASP .NET will automatically expire once it returns the result for the consecutive request. That means it is there until the target view is loaded fully. Once the session time out occurs this session will get expired automatically.
Maintaining the State of TempData in Asp.Net MVC
So in this case when we want to make values available for the third request as well. In this case, we need to maintain the state of tempdata on the second request. We can use the keep function with variables of TempData to fulfill this purpose.
Recommended Articles
This is a guide to TempData in MVC. Here we also discuss the introduction and how long does TempData last in MVC? along with different examples and its code implementation. you may also have a look at the following articles to learn more –