Introduction
In Asp.Net MVC there are three ways to pass/store data between the controllers and views.
ViewData
- ViewData is used to pass data from the controller to the view.
- It is derived from the ViewDataDictionary class.
- It is available for the current request only.
- Requires typecasting for complex data type and checks for null values to avoid error.
- If redirection occurs, then its value becomes null.
ViewBag
- ViewBag is also used to pass data from the controller to the respective view.
- ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- It is also available for the current request only.
- If redirection occurs, then its value becomes null.
- Doesn’t require typecasting for complex data types.
TempData
- TempData is derived from the TempDataDictionary class.
- TempData is used to pass data from the current request to the next request.
- It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action.
- It requires typecasting for complex data types and checks for null values to avoid errors. Generally, it is used to store only one-time messages like error messages and validation messages.


Sridhar YelagandhulaPosted Feb 22, 2023, 1:38 PM EST
Nice clear explanation!!!!
Sandeep SiddhaparaPosted Oct 5, 2020, 8:10 PM EDT
Very Nice Article
Kumaresan MurugesanPosted Mar 8, 2020, 4:47 AM EDT
Nice Article
satyesh soniPosted Nov 13, 2017, 8:08 PM EST
Nice articles.....
Gopaiah NekarukantiPosted Aug 18, 2017, 10:59 PM EDT
Nice information.....
Dhaval RathodPosted Apr 19, 2017, 11:04 PM EDT
Very helpful for beginners !!
kalu singh raoPosted Jul 8, 2016, 9:59 AM EDT
Nice...
manish nainPosted May 30, 2016, 3:59 AM EDT
www.stucorner.com
sakthi sudhanPosted Mar 24, 2016, 12:43 AM EDT
good
Santhakumar MunuswamyPosted Apr 25, 2015, 3:18 AM EDT
Good
Rohit ChoudharyPosted Apr 25, 2015, 2:42 AM EDT
<html>
Rasmiranjan sahooPosted Apr 6, 2015, 4:24 AM EDT
Nice post.