Skip to content

Instantly share code, notes, and snippets.

@teocci
Last active June 10, 2021 19:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teocci/1a301a21bac7b768bbecf7af26d7000d to your computer and use it in GitHub Desktop.
Save teocci/1a301a21bac7b768bbecf7af26d7000d to your computer and use it in GitHub Desktop.
In ASP.NET MVC there are three ways - ViewData, ViewBag and TempData to pass data from controller to view and in next request.

ViewData vs ViewBag vs TempData vs Session

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

ViewData

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

public ViewDataDictionary ViewData { get; set; }

ViewData is a property of ControllerBase class. ViewData is used to pass data from controller to corresponding view. It’s life lies only during the current request. If redirection occurs then it’s value becomes null. It’s required typecasting for getting data and check for null values to avoid error.

ViewBag

ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0. Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.

public Object ViewBag { get; }

ViewBag is a property of ControllerBase class. It’s life also lies only during the current request. If redirection occurs then it’s value becomes null. It doesn’t required typecasting for getting data.

alt text

TempData

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

public TempDataDictionary TempData { get; set; }

TempData is a property of ControllerBase class. TempData is used to pass data from current request to subsequent request (means redirecting from one page to another). It’s life is very short and lies only till the target view is fully loaded. It’s required typecasting for getting data and check for null values to avoid error. It is used to store only one time messages like error messages, validation messages. To persist data with TempData refer this article: Persisting Data with TempData

Session

In ASP.NET MVC, Session is a property of 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). Session is valid for all requests, not for a single redirect. It’s also required typecasting for getting data and check for null values to avoid error.

Summary

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment