Browse Articles

Intermediate

ViewData vs ViewBag vs TempData vs Session

09 Jan 2024
Intermediate
404K Views
5 min read  

ViewData vs ViewBag vs TempData vs Session: An Overview

In ASP.NET MVC there are three ways - ViewData, ViewBag, and TempData to pass data from the controller to view and in 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 its importance. In this article, I will try to explain the differences among these four.

ViewData

  1. ViewData is a dictionary object that is derived from the ViewDataDictionary class.

    public ViewDataDictionary ViewData { get; set; }
    
  2. ViewData is a property of the ControllerBase class.

  3. ViewData is used to pass data from the controller to the corresponding view.

  4. Its life lies only during the current request.

  5. If redirection occurs then its value becomes null.

  6. It’s required typecasting for getting data and checking for null values to avoid errors.

View Data Example:

//Controller Code
public ActionResult Index()
{
      List Employee= new List();
      Employee.Add("Komal");
      Employee.Add("Vishal");
      Employee.Add("Rahul");

      ViewData["Employee"] = Employee;
      return View();
}
//page code
    <% foreach (var employee in ViewData["Employee"] as List) { %> <%: employee%> <% } %>

ViewBag

  1. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.

  2. It is a wrapper around the ViewData and is also used to pass data from the controller to the corresponding view.

    public Object ViewBag { get; }
    
  3. ViewBag is a property of the ControllerBase class.

  4. Its life also lies only during the current request.

  5. If redirection occurs then its value becomes null.

  6. It doesn’t require typecasting for getting data.

ViewBag Example:

//Controller Code
public ActionResult Index()
{
      List Employee= new List();
      Employee.Add("Komal");
      Employee.Add("Vishal");
      Employee.Add("Rahul");

      ViewBag.Employee= Employee;
      return View();
}
//page code
    <% foreach (var student in ViewBag.Employee) { %> <%: employee%> <% } %>

TempData

  1. TempData is a dictionary object that is derived from the TempDataDictionary class and stored in a short lives session.

    public TempDataDictionary TempData { get; set; }
    
  2. TempData is a property of the ControllerBase class.

  3. TempData is used to pass data from the current request to subsequent requests (which means redirecting from one page to another).

  4. Its life is very short and lies only till the target view is fully loaded.

  5. It’s required typecasting for getting data and checking for null values to avoid errors.

  6. It is used to store only one-time messages like error messages, and validation messages. To persist data with TempData refer to this article: Persisting Data with TempData

Temp Data Example:


//Controller Code
public ActionResult Index()
{
    List Employee= new List();
    Employee.Add("Komal");
    Employee.Add("Vishal");
    Employee.Add("Rahul");

    TempData["Employee"] = Employee;
    return View();
}
//page code
    <% foreach (var employee in TempData["Employee"] as List) { %> <%: student%> <% } %>

Session

  1. In ASP.NET MVC, Session is a property of the Controller class whose type is HttpSessionStateBase.

    public HttpSessionStateBase Session { get; }
    
  2. 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 required typecasting for getting data and checking for null values to avoid errors.

Summary:

In this article, I tried to explain the difference between ViewData, ViewBag, and TempData. I hope you will refer to this article for your needs. I would like to have feedback from my blog readers. Please post your feedback, questions, or comments about this article.

Unlock the next level of MVC:

FAQs

Q1. What are the differences between ViewData ViewBag TempData and session?

Both 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.

Q2. Which is better ViewBag or ViewData?

 ViewData is faster than ViewBag.
Share Article
About Author
Shailendra Chauhan (Microsoft MVP, Founder & CEO at Scholarhat by DotNetTricks)

Shailendra Chauhan is the Founder and CEO at ScholarHat by DotNetTricks which is a brand when it comes to e-Learning. He provides training and consultation over an array of technologies like Cloud, .NET, Angular, React, Node, Microservices, Containers and Mobile Apps development. He has been awarded Microsoft MVP 8th time in a row (2016-2023). He has changed many lives with his writings and unique training programs. He has a number of most sought-after books to his name which has helped job aspirants in cracking tough interviews with ease.
Learn to Crack Your Technical Interview

Accept cookies & close this