Difference between ViewBag vs ViewData vs TempData vs Session in MVC

Today we will compare ways to return value from Controller to View in ASP.Net MVC. Basically there are 4 ways to transfer data or object from Controller to View in ASP.Net MVC. Which are ViewBag, ViewData, TempData and Session. We will also see how we can transfer data or object from one controller to another controller action method.

Difference between ViewBag vs ViewData vs TempData vs Session in MVC
Difference between ViewBag vs ViewData vs TempData vs Session in MVC 
1. ViewData in MVC
  • ViewData is property of ControllerBase class.
  • ViewData is a type of dictionary object.
  • ViewData is key-value dictionary collection.
  • ViewData was introduced in MVC 1.0 version.
  • ViewData works with .Net framework 3.5 and above.
  • Need to do type conversion of code while enumerating.
  • ViewData object keeps data only for current request.
  • Below is the example of ViewData in ASP.Net MVC.
//ViewData Example: Controller Source Code:
  1.  
  2. public ActionResult Index()  
  3. {  
  4.       List<string> Languages = new List<string>();  
  5.       Languages.Add("ASP.Net");  
  6.       Languages.Add("Java");  
  7.       Languages.Add("Python");  
  8.    
  9.       ViewData["Languages"] = Languages;  
  10.       return View();  
  11. }  

//ViewData Example: Page Source Code
  1.  
  2. <ul>  
  3.     <% foreach (var language in ViewData["Languages"] as List<string>)  
  4.         { %>  
  5.     <li><%: language%></li>  
  6.     <% } %>  
  7. </ul>

2. ViewBag in MVC
  • ViewBag is property of ControllerBase class.
  • ViewBag is a type of dynamic object.
  • ViewBag is a type of object.
  • ViewBag was introduced in MVC 3.0 version.
  • ViewBag works with .Net framework 4.0 and above.
  • ViewBag uses property and handles it, so no need to do type conversion while enumerating.
  • ViewBag object keeps data only for current request.
//ViewBag Example: Controller Source Code 
  1.  
  2. public ActionResult Index()  
  3. {  
  4.       List<string> Languages = new List<string>();  
  5.       Languages.Add("ASP.Net");  
  6.       Languages.Add("Java");  
  7.       Languages.Add("Python");  
  8.    
  9.       ViewBag.Languages = Languages;  
  10.       return View();  
  11. }   
//ViewBag Example: Page Source Code
  1. <ul>  
  2.     <% foreach (var language in ViewBag.Languages)  
  3.         { %>  
  4.     <li><%: language%></li>  
  5.     <% } %>  
  6. </ul> 

3. TempData in MVC
  • TempData is property of ControllerBase class.
  • TempData is a type of dictionary object.
  • TempData is key-value dictionary collection.
  • TempData was introduced in MVC 1.0 version.
  • TempData works with .Net framework 3.5 and above.
  • Need to do type conversion of code while enumerating.
  • TempData object is used to data between current request and subsequent request.
//TempData Example: Controller Source Code
  1. public ActionResult Index()  
  2. {  
  3.     List<string> Languages = new List<string>();  
  4.     Languages.Add("ASP.Net");  
  5.     Languages.Add("Java");  
  6.     Languages.Add("Python");  
  7.    
  8.     TempData["Student"] = Student;  
  9.     return View();  
  10. }  
//TempData Example: Page Source Code
  1. <ul>  
  2.     <% foreach (var language in TempData["Languages"] as List<string>)  
  3.         { %>  
  4.     <li><%: language%></li>  
  5.     <% } %>  
  6. </ul>

4. Session in MVC
  • Session is property of Controller(Abstract Class).
  • Session is a type of HttpSessionStateBase.
  • Session is key-value dictionary collection.
  • Session was introduced in MVC 1.0 version.
  • TempData works with .Net framework 1.0 and above.
  • Need to do type conversion of code while enumerating.
  • Session object keeps data for all requests. Valid for all requests, never expires.
//Session Example: Controller Source Code
  1. public ActionResult Index()  
  2.        {  
  3.          
  4.            Session["Languages"] = new List<string>() { "ASP.Net""Java""Python"};  
  5.            return View();  
  6.        }
//Session Example: Page Source Code
  1. <ul>  
  2.     <% foreach (var language in (List<string>)Session["Languages"])  
  3.         { %>  
  4.     <li><%: language%></li>  
  5.     <% } %>  
  6. </ul>

Post a Comment

0 Comments