Click here to Skip to main content
15,881,858 members
Articles / MVC
Article

Using TempData in ASP.NET MVC

Rate me:
Please Sign up or sign in to vote.
3.07/5 (15 votes)
16 Jun 2014CPOL2 min read 165.5K   15   4
In previous ASP.NET MVC Tutorial, we discussed about different available options for passing data from Controller to View in ASP.NET MVC. We implemented passing data using ViewBag and ViewData in ASP.NET MVC application. Now, in this part of the tutorial, we are going to discuss about the third avai

In previous ASP.NET MVC Tutorial, we discussed about different available options for passing data from Controller to View in ASP.NET MVC. We implemented passing data using ViewBag and ViewData in ASP.NET MVC application. Now, in this part of the tutorial, we are going to discuss about the third available option i.e. TempData.

TempData in ASP.NET MVC is basically a dictionary object derived from TempDataDictionary. TempData stays for a subsequent HTTP Request as opposed to other options (ViewBag and ViewData) those stay only for current request. So, TempdData can be used to maintain data between controller actions as well as redirects.

Note: Just like ViewData, typecasting and null checks required for TempData also in order to avoid errors.

Let’s see how we can use TempData in a practical scenario to pass data from one controller action to another.

 //Controller Action 1 (TemporaryEmployee)
 public ActionResult TemporaryEmployee()
{
                Employee employee = new Employee
                {
                        EmpID = "121",
                        EmpFirstName = "Imran",
                        EmpLastName = "Ghani"
                };
                TempData["Employee"] = employee;
                return RedirectToAction("PermanentEmployee");
}

 //Controller Action 2(PermanentEmployee)
 public ActionResult PermanentEmployee()
{
               Employee employee = TempData["Employee"] as Employee;
               return View(employee);
 }

As in above example, we store an employee object in TempData in Controller Action 1 (i.e. TemporaryEmployee) and retrieve it in another Controller Action 2 (i.e. PermanentEmployee). But If we try to do the same using ViewBag or ViewData, we will get null in Controller Action 2 because only TempData object maintains data between controller actions.

An important thing about TempData is that it stores contents in Session object. Then one may raise a question that “What’s the difference between TempData in ASP.NET MVC and Session?” or “Why we have TempData dictionary object?, Why can’t we use the Session object directly?

ASP.NET MVC TempData Vs Sessions

Although ASP.NET MVC TempData stores it’s content in Session state but it gets destroyed earlier than a session object. TempData gets destroyed immediately after it’s used in subsequent HTTP request, so no explicit action required. If we use a Session object for this purpose, we would have to destroy the object explicitly.

It’s best fit for scenarios when:

  • we need to preserve data from current to subsequent request.
  • passing error message to an error page.

With the above discussion on using TempData in ASP.NET MVC, we have successfully covered different options for passing data from Controller to View in ASP.NET MVC. Hopefully reader will have a better understanding of using ViewBag, ViewData and TempData in ASP.NET MVC.

Previous: ViewBag and ViewData in ASP.NET MVC

Other Related Articles

The post Using TempData in ASP.NET MVC appeared first on Web Development Tutorial.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
GeneralGood One... Thanks Pin
aarif moh shaikh4-Sep-15 1:17
professionalaarif moh shaikh4-Sep-15 1:17 
GeneralMy vote of 1 Pin
Member 1169861811-Jun-15 18:42
Member 1169861811-Jun-15 18:42 
QuestionPassing TempData to another View Pin
prams_hi8-May-15 1:27
prams_hi8-May-15 1:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.