ViewData VS ViewBag Vs TempData in MVC

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

  1. //Controller Code  
  2. public ActionResult Index()  
  3. {  
  4.       List<string> Student = new List<string>();  
  5.       Student.Add("Jignesh");  
  6.       Student.Add("Tejas");  
  7.       Student.Add("Rakesh");  
  8.    
  9.       ViewData["Student"] = Student;  
  10.       return View();  
  11. }  
  12. //page code  
  13. <ul>  
  14.     <% foreach (var student in ViewData["Student"] as List<string>)  
  15.         { %>  
  16.     <li><%: student%></li>  
  17.     <% } %>  
  18. </ul>  

ViewBag Example

  1. //Controller Code  
  2. public ActionResult Index()  
  3. {  
  4.       List<string> Student = new List<string>();  
  5.       Student.Add("Jignesh");  
  6.       Student.Add("Tejas");  
  7.       Student.Add("Rakesh");  
  8.    
  9.       ViewBag.Student = Student;  
  10.       return View();  
  11. }   
  12. //page code  
  13. <ul>  
  14.     <% foreach (var student in ViewBag.Student)  
  15.         { %>  
  16.     <li><%: student%></li>  
  17.     <% } %>  
  18. </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

  1. //Controller Code  
  2. public ActionResult Index()  
  3. {  
  4.     List<string> Student = new List<string>();  
  5.     Student.Add("Jignesh");  
  6.     Student.Add("Tejas");  
  7.     Student.Add("Rakesh");  
  8.    
  9.     TempData["Student"] = Student;  
  10.     return View();  
  11. }  
  12. //page code  
  13. <ul>  
  14.     <% foreach (var student in TempData["Student"] as List<string>)  
  15.         { %>  
  16.     <li><%: student%></li>  
  17.     <% } %>  
  18. </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.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now