30
MarViewData vs ViewBag vs TempData vs Session
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
ViewData is a dictionary object that is derived from the ViewDataDictionary class.
public ViewDataDictionary ViewData { get; set; }
ViewData is a property of the ControllerBase class.
ViewData is used to pass data from the controller to the corresponding view.
Its life lies only during the current request.
If redirection occurs then its value becomes null.
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
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
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; }
ViewBag is a property of the ControllerBase class.
Its life also lies only during the current request.
If redirection occurs then its value becomes null.
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
TempData is a dictionary object that is derived from the TempDataDictionary class and stored in a short lives session.
public TempDataDictionary TempData { get; set; }
TempData is a property of the ControllerBase class.
TempData is used to pass data from the current request to subsequent requests (which means redirecting from one page to another).
Its life is very short and lies only till the target view is fully loaded.
It’s required typecasting for getting data and checking for null values to avoid errors.
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
In ASP.NET MVC, Session is a property of the Controller class whose type is HttpSessionStateBase.
public HttpSessionStateBase Session { get; }
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).
The session is valid for all requests, not for a single redirect.
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.