Difference Between ViewData, ViewBag, TempData, And Session In MVC 5

Introduction

Before we discuss the difference between ViewData, ViewBag, TempData, and Session. Let's learn about the main use of transferring data from the Controller to View in MVC. ViewData, ViewBag, TempData, and Session are weak types, which means if you misspell a variable name it throws an error in runtime, not compile time.

Transporting data from the controller to view

A controller is responsible for handling overall communication between model and view. It will access data from the model and send to View. They are:
  1. ViewData
  2. ViewBag
  3. TempData
  4. Session 
View Data
  • It is a property of ControllerBase (Abstract class) class
  • It is a type of ViewDataDictionary
  • It is dictionary type collection  with key and value pair

    Syntex

    ViewData.Add("Employees", "Ankit Sahu");
    OR
    ViewData["Employees"] = "Ankit Sahu";

  • It cannot return complex values directly on a callback
  • It is available for the current request.Data in ViewData  and is not available across the request 
  • It's present in MVC framework 1.0 
  • ViewData is faster in compression to ViewBag
  • Its value becomes NULL if redirection has occurred. 
  1. public abstract class ControllerBase: IController  
  2. {         
  3.     public ViewDataDictionary ViewData { getset; }         
  4. }  
Step 1 

Add DemoController in MVC application 
  1. public class DemoController : Controller  
  2.    {  
  3.        // GET: Demo  
  4.        public ActionResult Index()  
  5.        {  
  6.            ViewData["EmployeeName"] = "Ankit Sahu";  
  7.            ViewData["KnownTechnology"] = new List<string>() { "ASP.Net""SQL Server""MVC""Angular" };  
  8.            return View();  
  9.        }  
  10.    }  
Step 2

Add Index View
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. <h2>Index</h2>  
  5. Employee Name :- @ViewData["EmployeeName"];  
  6. <br />  
  7. Known Technologys :-  
  8. <ul>  
  9.     @foreach (var technology in (List<string>)ViewData["KnownTechnology"])  
  10.     {  
  11.         <li>  
  12.             @technology  
  13.         </li>  
  14.     }  
  15. </ul>  
ViewBag
  • It is a property of ControllerBase (Abstract class) class
  • It is a dynamic
     type
  • Dynamic type stores values in implicitly typed references. Hence they are resolved only in runtime.
Syntex

ViewBag.
Employees=
"Ankit Sahu";
  • It can return complex values directly on a callback.
  • It is available for the current request. Data in ViewBag is not available across the request
  • It's present in MVC framework 3.0 
  • ViewBag is slower in compression to ViewData
  • Its value becomes NULL if redirection has occurred.
  1. public abstract class ControllerBase: IController
  2. {
  3. public dynamic ViewBag { get; }
  4. }
Step 1

Add DemoController in MVC application
  1. public class DemoController : Controller  
  2.    {  
  3.        // GET: Demo  
  4.        public ActionResult Index()  
  5.        {  
  6.            ViewBag.EmployeeName = "Ankit Sahu";  
  7.            ViewBag.KnownTechnology = new List<string>() { "ASP.Net""SQL Server""MVC""Angular" };  
  8.            return View();  
  9.        }  
  10.    }  
Step 2

Add Index View
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. <h2>Index</h2>  
  5. Employee Name :- @ViewBag.EmployeeName;  
  6. <br />  
  7. Known Technologys :-  
  8. <ul>  
  9.     @foreach (var technology in ViewBag.KnownTechnology)  
  10.     {  
  11.         <li>  
  12.             @technology  
  13.         </li>  
  14.     }  
  15. </ul>  
TempData
  • It is a property of ControllerBase (Abstract class) class
  • It is a type of TempDataDictionary
  • It is a dictionary type collection with key and value pair

    Syntex

    TempData.Add("Employees", "Ankit Sahu");
    OR
    TempData["Employees"] = "Ankit Sahu";

  • It cannot return complex values directly on a callback.
  • It is available for the current request. Data in TempData is not available across the request.
  • It is implicit and available only for all context 
  • It's present in MVC framework 1.0 
  • Call TempData.Keep() to keep all the values of TempData in a third request. 
  1. public abstract class ControllerBase: IController  
  2. {  
  3. public TempDataDictionary TempData{ getset; }  
  4. }  
Step 1 

Add DemoController in MVC application
  1. public class DemoController : Controller  
  2.   {  
  3.       // GET: Demo  
  4.       public ActionResult Index()  
  5.       {  
  6.           TempData["EmployeeName"] = "Ankit Sahu";  
  7.           TempData["KnownTechnology"] = new List<string>() { "ASP.Net""SQL Server""MVC""Angular" };  
  8.           return View();  
  9.       }  
  10.   }  
Step 2

Add Index View
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. <h2>Index</h2>  
  5. Employee Name :- @TempData["EmployeeName"];  
  6. <br />  
  7. Known Technologys :-  
  8. <ul>  
  9.     @foreach (var technology in (List<string>)TempData["KnownTechnology"])  
  10.     {  
  11.         <li>  
  12.             @technology  
  13.         </li>  
  14.     }  
  15. </ul>  
Session
  • It is a property of Controller(Abstract class) class
  • It is a type of HttpSessionStateBase 
  • It is dictionary type collection with key and value pair

    Syntex

    Session .Add("Employees", "Ankit Sahu");
    OR
    Session ["Employees"] = "Ankit Sahu";

  • It cannot return complex values directly on a callback
  • It is available for the current request. Data in Session data is available across the request
  • It's present from MVC framework 1.0 
  • All session information is stored in server side. 
  1. public abstract class ControllerBase: IController        
  2. {        
  3. public HttpSessionStateBase Session { get; }      
  4. }   
Example:

Step 1

Add HomeController in MVC application 
  1. public class HomeController : Controller  
  2.    {  
  3.        public ActionResult Index()  
  4.        {  
  5.            Session["EmployeeName"] = "Ankit Sahu";  
  6.            Session["KnownTechnology"] = new List<string>() { "ASP.Net""SQL Server""MVC""Angular" };  
  7.            return View();  
  8.        }    
  9.    }  
Step 2

Add View Controller 
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <h2>Index</h2>  
  6. <br>  
  7. Employee Name :- @Session["EmployeeName"];    
  8. <br />  
  9. Known Technologys :-  
  10. <ul>  
  11.     @foreach (var technology in (List<string>)Session["KnownTechnology"])  
  12.     {  
  13.         <li>  
  14.             @technology  
  15.         </li>  
  16.     }  
  17. </ul>  
  18.     
Thank you for reading! !!! 
Next Recommended Reading ViewData VS ViewBag Vs TempData in MVC