Please enable Javascript to correctly display the contents on Dot Net Tricks!

ViewData vs ViewBag vs TempData vs Session

Posted By : Shailendra Chauhan, 19 Jul 2012
Updated On : 05 Apr 2015
Total Views : 260,920   
Support : MVC5, MVC4 & MVC3
 

In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view and in next request. Like WebForm, you can also use Session to persist data during a user session. Now question is that when to use ViewData, VieBag, TempData and Session. Each of them has its own importance. In this article, I am trying to explain the differences among these four.

ViewData

  1. ViewData is a dictionary object that is derived from ViewDataDictionary class.

    public ViewDataDictionary ViewData { get; set; }
    
  2. ViewData is a property of ControllerBase class.

  3. ViewData is used to pass data from controller to corresponding view.

  4. It’s life lies only during the current request.

  5. If redirection occurs then it’s value becomes null.

  6. It’s required typecasting for getting data and check for null values to avoid error.

ViewBag

  1. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.

  2. Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.

    public Object ViewBag { get; }
    
  3. ViewBag is a property of ControllerBase class.

  4. It’s life also lies only during the current request.

  5. If redirection occurs then it’s value becomes null.

  6. It doesn’t required typecasting for getting data.

TempData

  1. TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.

    public TempDataDictionary TempData { get; set; }
    
  2. TempData is a property of ControllerBase class.

  3. TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).

  4. It’s life is very short and lies only till the target view is fully loaded.

  5. It’s required typecasting for getting data and check for null values to avoid error.

  6. It is used to store only one time messages like error messages, validation messages. To persist data with TempData refer this article: Persisting Data with TempData

Session

  1. In ASP.NET MVC, Session is a property of Controller class whose type is HttpSessionStateBase.

    public HttpSessionStateBase Session { get; }
    
  2. Session is also used to pass data within the ASP.NET MVC application and Unlike TempData, it persists for its expiration time (by default session expiration time is 20 minutes but it can be increased).

  3. Session is valid for all requests, not for a single redirect.

  4. It’s also required typecasting for getting data and check for null values to avoid error.

Summary

In this article I try to explain the difference between ViewData, ViewBag and TempData. I hope you will refer this article for your need. I would like to have feedback from my blog readers. Please post your feedback, question, or comments about this article.

 
Recommended for you
 
About the Author
Shailendra Chauhan

Shailendra Chauhan is an Entrepreneur, Author, Architect, and Corporate Trainer. He has rewarded as Microsoft MVP for his exceptional contributions in Microsoft Visual Studio and Development Technologies.

With more than 7 years in hand experience Shailendra Chauhan is a polymath in the domains of Microsoft .NET technologies and an array of other technologies including JavaScript, AngularJS, Node.js, Ionic and NoSQL Databases to name but a few.

He is the author of some of most popular e-books which encompass technical Interview on Node.js Interview Questions and Answers , ASP.NET MVC Interview Questions and Answers , AngularJS Interview Questions and Answers and LINQ Interview Questions and Answers. Furthermore he is a technical reviewer for book on ASP.NET MVC 4 Mobile App Development. Know more...
 
Free Interview Books
 
28 AUG
NodeJS Development (offline)

Sat, Sun 11:00 AM-12:30 PM

More Details
8 AUG
ASP.NET MVC with AngularJS Development (online)

Monday 09:30 PM-11:00 PM IST

More Details
4 AUG
NodeJS Development (online)

Thursday 06:00 AM-7:30 AM IST

More Details
1 AUG
AngularJS Development (online)

Mon-Fri 08:00 PM-09:30 PM IST

More Details
1 AUG
ASP.NET MVC with AngularJS Development (online)

Mon-Fri (07:30 AM-09:00 AM IST)

More Details
24 JUL
AngularJS Development (offline)

Sat,Sun     (08:00 AM-09:30 AM)

24 JUL
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     (09:30 AM-11:00 AM)

28 MAY
ASP.NET MVC with AngularJS Development (offline)

Sat, Sun     (05:00 PM-07:00 PM IST)

30 APR
NodeJS Development (offline)

Sat, Sun     (11:00 AM-12:30 PM IST)

BROWSE BY CATEGORY
 
SUBSCRIBE TO LATEST NEWS
 
LIKE US ON FACEBOOK
 
+