Main Difference Between ViewData Vs ViewBag Vs TempData In ASP.NET MVC

In ASP.NET MVC there are three ways - ViewData, ViewBag, and TempData - to pass data from the controller to view the next request. Like WebForm, you can also use Session to persist data during a user session. Now, the question is when to use ViewData, VieBag, TempData, and Session. Each of them has their own importance. In this article, I am trying to explain the differences among these four.

ViewData
  1. ViewData is derived from the ViewDataDictionary class and is basically a Dictionary object i.e., Keys and Values where Keys are String while Values will be objects.
  2. Data is stored as Object in ViewData.
  3. While retrieving the data it needs to be Type Cast to its original type as the data is stored as the object and it also requires NULL checks while retrieving.
  4. ViewData is used for passing value from Controller to View.
  5. ViewData is available only for Current Request. It will be destroyed on redirection.
Example

In the below example, a string value is set in the ViewData object in Controller and it is then displayed in View.

Controller
  1. public class FirstController: Controller {  
  2.     // GET: First  
  3.     public ActionResult Index() {  
  4.         ViewData["Message"] = "Hello RAJESH!";  
  5.         return View();  
  6.     }  
  7. }  
 View
  1. <html>  
  2.   
  3. <head>  
  4.     <meta name="viewport" content="width=device-width" />  
  5.     <title>Index</title>  
  6. </head>  
  7.   
  8. <body>  
  9.     <div> @ViewData["Message"] </div>  
  10. </body>  
  11.   
  12. </html> 
ViewBag
  1. ViewBag is a Wrapper built around ViewData.
  2. ViewBag is a dynamic property and it makes use of the C# 4.0 dynamic features.
  3. While retrieving, there is no need for Type Casting data.
  4. ViewBag is used for passing value from Controller to View.
  5. ViewBag is available only for Current Request. It will be destroyed on redirection.
Example

In the below example, a string value is set in the ViewBag object in Controller and it is then displayed in View.

Controller
  1. public class FirstController: Controller {  
  2.     // GET: First  
  3.     public ActionResult Index() {  
  4.         ViewBag.Message = "Hello RAJESH!";  
  5.         return View();  
  6.     }  
  7. }  
 View
  1. <html>  
  2.   
  3. <head>  
  4.     <meta name="viewport" content="width=device-width" />  
  5.     <title>Index</title>  
  6. </head>  
  7.   
  8. <body>  
  9.     <div> @ViewBag.Message </div>  
  10. </body>  
  11.   
  12. </html>  
TempData
  1. TempData is derived from the TempDataDictionary class and is basically a Dictionary object i.e. Keys and Values where Keys are String while Values will be objects.
  2. Data is stored as Object in TempData.
  3. While retrieving the data it needs to be Type Cast to its original type as the data is stored as objects and it also requires NULL checks while retrieving.
  4. TempData can be used for passing value from Controller to View and also from Controller to Controller.
  5. TempData is available for Current and Subsequent Requests. It will not be destroyed on redirection.
Example

In the below example, a string value is a set of the TempData objects in Controller and they have redirected to another Controller and finally, it is displayed in View.

First Controller
  1. public class FirstController: Controller {  
  2.         // GET: First  
  3.         public ActionResult Index() {  
  4.             TempData["Message"] = "Hello RAJESH!";  
  5.             return new RedirectResult(@ "~\Second\");  
  6.             }  
  7.         }  
 Second Controller
  1. <html>  
  2.   
  3. <head>  
  4.     <meta name="viewport" content="width=device-width" />  
  5.     <title>Index</title>  
  6. </head>  
  7.   
  8. <body>  
  9.     <div> @TempData["Message"]; </div>  
  10. </body>  
  11.   
  12. </html>  
Session
  1. In ASP.NET MVC, Session is a property of Controller class whose type is HttpSessionStateBase.

    public HttpSessionStateBase Session { get; }

  2. The Session is also used to pass data within the ASP.NET MVC application and Unlike TempData, it persists for its expiration time (by default session expiration time is 20 minutes but it can be increased).

  3. The Session is valid for all requests, not for a single redirect.

  4.  It also requires typecasting for getting data and checks for null values to avoid error.
 
 Thank you.