ASP.NET MVC TempData & Session

By | June 11, 2014

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.

ASP.NET MVC TempData Tutorial

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.ViewData Vs ViewBag Vs TempData

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.

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?

Assembly that defines ASP.NET MVC framework [Choose One]:

  • A. System.Web.http
  • B. System.Web.mvcframework
  • C. System.Web.mvc
  • D. System.Web.mvc.helpers

To further test your ASP.NET MVC skill, Take a Complete FREE Online Test or MCSD Practice Exam: 70-486 (Developing ASP.NET MVC Web Applications). Simply Click Here.

Further If you are interested to enhance your skills in all related technologies used in this article Series and others, you MUST understand related concepts in these FAQs/Interview Questions Series.

 Correct Answer: C

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 ASP.NET MVC 5 Articles and Related:

Top 10 Interview Questions and Answers Series: