Introduction
ViewData and ViewBag are used for the same purpose -- to transfer data from controller to view. ViewData is nothing but a dictionary of objects and it is accessible by string as key. ViewData is a property of controller that exposes an instance of the ViewDataDictionary class. ViewBag is very similar to ViewData. ViewBag is a dynamic property (dynamic keyword which is introduced in .net framework 4.0). ViewBag is able to set and get value dynamically and able to add any number of additional fields without converting it to strongly typed. ViewBag is just a wrapper around the ViewData.
ViewData Example
//Controller Code
public ActionResult Index()
{
List<string> Student = new List<string>();
Student.Add("Jignesh");
Student.Add("Tejas");
Student.Add("Rakesh");
ViewData["Student"] = Student;
return View();
}
//page code
<ul>
<% foreach (var student in ViewData["Student"] as List<string>)
{ %>
<li><%: student%></li>
<% } %>
</ul>
ViewBag Example
//Controller Code
public ActionResult Index()
{
List<string> Student = new List<string>();
Student.Add("Jignesh");
Student.Add("Tejas");
Student.Add("Rakesh");
ViewBag.Student = Student;
return View();
}
//page code
<ul>
<% foreach (var student in ViewBag.Student)
{ %>
<li><%: student%></li>
<% } %>
</ul>TempData is a dictionary which is derived from TempDataDictionary class. TempData is stored data just like live session for short time. TempData keeps data for the time of HTTP Request, which means that it holds data between two consecutive requests. TempData helps us to transfer data between controllers or between actions. TempData internally use Session variables. Note that TempData is only work during the current and subsequent request. It is generally used to store one time messages. With the help of the TempData.Keep() method we can keep value in TempData object after request completion.
TempData Example
//Controller Code
public ActionResult Index()
{
List<string> Student = new List<string>();
Student.Add("Jignesh");
Student.Add("Tejas");
Student.Add("Rakesh");
TempData["Student"] = Student;
return View();
}
//page code
<ul>
<% foreach (var student in TempData["Student"] as List<string>)
{ %>
<li><%: student%></li>
<% } %>
</ul>
ViewData VS ViewBag VS TempData
| ViewData | ViewBag | TempData |
| It is Key-Value Dictionary collection | It is a type object | It is Key-Value Dictionary collection |
| ViewData is a dictionary object and it is property of ControllerBase class | ViewBag is Dynamic property of ControllerBase class. | TempData is a dictionary object and it is property of controllerBase class. |
| ViewData is Faster than ViewBag | ViewBag is slower than ViewData | NA |
| ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above | ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above | TempData is also introduced in MVC1.0 and available in MVC 1.0 and above. |
| ViewData also works with .net framework 3.5 and above | ViewBag only works with .net framework 4.0 and above | TempData also works with .net framework 3.5 and above |
| Type Conversion code is required while enumerating | In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating. | Type Conversion code is required while enumerating |
| Its value becomes null if redirection has occurred. | Same as ViewData | TempData is used to pass data between two consecutive requests. |
| It lies only during the current request. | Same as ViewData | TempData only works during the current and subsequent request |
Conclusion
We have three options: ViewData, ViewBag and TeampData for passing data from controller to view and in next request. ViewData and ViewBag are almost similar and it helps us to transfer the data from controller to view whereas TempData also works during the current and subsequent requests.


Mh AbirPosted Jul 23, 2023, 1:03 AM EDT
Nice Document
Shiv Ratan KumarPosted Aug 3, 2019, 9:19 AM EDT
I am not getting the main difference between ViewBag & ViewData. one required casting another not required its not a difference between them. Both are checking run time. Can you please explain??
Snehal WaghPosted Jul 21, 2019, 5:18 AM EDT
What viewdata,viewbag will return you??
Snehal WaghPosted Jul 21, 2019, 5:18 AM EDT
What viewdata,viewbag will return you???
Kishor TalaPosted Apr 18, 2018, 9:30 PM EDT
Thanks for sharing comparison in tabular format.
Prachi PurwarPosted Feb 14, 2018, 7:36 AM EST
Thanks for sharing it.
Dennis ThomasPosted Jan 23, 2018, 12:19 AM EST
Good one Jignesh! Thank you...
Manav PandyaPosted May 30, 2017, 8:32 PM EDT
Thanks for sharing sir .................
Rohit KumarPosted May 26, 2017, 10:43 AM EDT
3. Read normally n call keep () method then it also persistent value. 4. Read via peek () method
Rohit KumarPosted May 26, 2017, 10:42 AM EDT
1. If u didn't read Value then it persistent value for next request. 2. If u read normally then it doesn't persistent value
Rohit KumarPosted May 26, 2017, 10:40 AM EDT
TempData persistent data for next request depends upon 4 conditions
Rohit KumarPosted May 26, 2017, 10:38 AM EDT
If u read data via peek() method then it will also persistent data for next request.
Saravanan VPosted May 26, 2017, 9:10 AM EDT
Simple and Nice...
Rehman ShahidPosted May 21, 2017, 2:48 AM EDT
How ViewData is faster than ViewBag..????
Dipen PatelPosted Nov 4, 2016, 8:20 PM EDT
If ViewData and viewBag doing same task.which should we use and why?
Dipen PatelPosted Nov 4, 2016, 8:20 PM EDT
If viewdata and viewbag is almost same then which choose and why?
Anil Kumar MurmuPosted Sep 8, 2016, 8:16 AM EDT
Thanks for sharing the knowledge.
kalu singh raoPosted Jul 8, 2016, 10:01 AM EDT
Nice...
Ashish JoshiPosted Jan 21, 2016, 12:00 AM EST
Very helpful article for both beginer as well experienced MVC players
Ravi PatelPosted Mar 25, 2015, 9:15 PM EDT
great
sreenivasa kPosted Mar 10, 2015, 8:15 AM EDT
good
Venkat KumarPosted Feb 16, 2015, 7:48 PM EST
nice
Salma jahanPosted Jun 18, 2014, 2:49 AM EDT
awesome
Pramod LawatePosted Feb 20, 2014, 9:32 PM EST
nice
Akhil MittalPosted Jul 20, 2013, 11:21 PM EDT
nice article