Difference Between ViewData, ViewBag, TemData and Session in MVC

ViewData

ViewData is a dictionary object derived from the ViewDataDictionary class. Viewdata passes data between a Controller and a View. Its life lies only during the current request. If redirection occurs then its value become null. Viewdata is a property of the controller class.

Let  us see an example.

Add a model class to your project.

  1. public class HomeController : Controller    
  2. {    
  3.             
  4.     public ActionResult ShowData()    
  5.     {    
  6.                
  7.         ViewData["Datetime"] = DateTime.Now;    
  8.         ViewData["name"] = "hello my name is taj";    
  9.         return  view();    
  10.     }    
  11. }  
View page Code
  1. <html>    
  2. <head>    
  3.     <meta name="viewport" content="width=device-width" />    
  4.     <title>ShowData</title>    
  5. </head>    
  6. <body>    
  7.     <div>     
  8.      @ViewData["Datetime"] <br />    
  9.      @ViewData["name"]            
  10.     </div>    
  11. </body>    
  12. </html>   
Add the breakpoint on ShowData() and to check the flow of data press F11.



Let  see the output of preceding code.

 
ViewBag

A Viewbag is a collection of viewdata. A Viewbag is a dynamic property that benefits from a new feature of C# 4.0. Viewbag doesn't require typecasting for complex datatype. Viewbag is a property of the controller class. Its life exists only during the current request. If redirection occurs then its value become null.



Let us see an example.
  1. public class HomeController : Controller  
  2. {  
  3.         
  4.     public ActionResult ShowData()  
  5.     {  
  6.         ViewBag.datetime = DateTime.Now;  
  7.         ViewBag.name = "hello ia m view bag";  
  8.         return view();  
  9.     }  
  10. }  
The following is the View page code:
  1. <html>  
  2. <head>  
  3.     <meta name="viewport" content="width=device-width" />  
  4.     <title>ShowData</title>  
  5. </head>  
  6. <body>  
  7.     <div>   
  8.    @ViewBag.datetime<br/>  
  9.    @ViewBag.name  
  10.         </div>  
  11. </body>  
  12. </html>  
The following is the output of the preceding code:


Temp Data

Tempdata is a dictionary object derived from the TempDataDictionary class and is stored in a short live session. TempData is a property of the Controller class. It's life is very short and exists only until the target view is fully loaded. It's required typecasting for getting the data and to check for null values to avoid errors. Temdata cannot pass the data from the Controller to the View. TempData persists the data ActionResult to ActionResult.

Let us see an example.
  1. public class HomeController : Controller  
  2. {  
  3.           
  4.     public ActionResult ShowData()  
  5.     {  
  6.        TempData["datetime"] = DateTime.Now;  
  7.        TempData["name"]="hello i am temp data";  
  8.        return   RedirectToAction("ShowData1");  
  9.     }  
  10.   
  11.   
  12.     public ActionResult ShowData1()  
  13.     {  
  14.         string str = TempData["datetime"].ToString();  
  15.         string str1 = TempData["name"].ToString();  
  16.         return View("ShowData");  
  17.     }  
  18. }  
Add the breakpoint on ShowData() and ShowData1() to check the flow of data then press F11.

Select str in the showdata method and right-click on str => click Quickwatch.



Session

Session is an object derived from the HttpSessionState class. Session is the property of the HttpContext class. Session can also pass the data from the controller to the view and Action to Action. Session is valid for all requests, not for a single redirect and it is also need typecasting to getting data and check for null values to avoid error.

Let us see an example.
  1. public class HomeController : Controller  
  2.     {  
  3.           
  4.         public ActionResult ShowData()  
  5.         {  
  6.               
  7.           Session["datetime"] = DateTime.Now;  
  8.            Session["name"] = "hello i am session";  
  9.             return   RedirectToAction("ShowData1");  
  10.   
  11.            }  
  12.   
  13.   
  14.         public ActionResult ShowData1()  
  15.         {  
  16.            string str = Session["datetime"].ToString();  
  17.             string str1 = Session["name"].ToString();  
  18.             Session["datetime"] = DateTime.Now;  
  19.             Session["name"] = "hello i am session";  
  20.             return View("ShowData");  
  21.           
  22.         }  
  23.     }  
The following is the View page code:
  1. <html>  
  2. <head>  
  3.     <meta name="viewport" content="width=device-width" />  
  4.     <title>ShowData</title>  
  5. </head>  
  6. <body>  
  7.     <div>   
  8.        @Session["datetime"]<br />  
  9.           
  10.     </div>  
  11. </body>  
  12. </html>  
Add the breakpoint on ShowData() and ShowData1() to check the flow of data then press F11.

Let us see in the QuickWatch.



In the preceding figure we saw the role of session. session passes the data from Controller to View and Action to Action.

The following is the output of the preceding code:


Summary



This article showed how to pass data from the Controller to the View and Action to Action using ViewBag, ViewData, TempData and Session.

Up Next
    Ebook Download
    View all
    Learn
    View all