Skip to main content

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

  1. ViewData is a dictionary object that is derived from ViewDataDictionary class.
    1. public ViewDataDictionary ViewData { get; set; }
  2. ViewData is a property of ControllerBase class.
  3. ViewData is used to pass data from controller to corresponding view.
  4. It’s life lies only during the current request.
  5. If redirection occurs then it’s value becomes null.
  6. It’s required typecasting for getting data and check for null values to avoid error.

ViewBag

  1. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
  2. Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
    1. public Object ViewBag { get; }
  3. ViewBag is a property of ControllerBase class.
  4. It’s life also lies only during the current request.
  5. If redirection occurs then it’s value becomes null.
  6. It doesn’t required typecasting for getting data.

TempData

  1. TempData is a dictionary object that is derived from TempDataDictionary class and stored in short lives session.
    1. public TempDataDictionary TempData { get; set; }
  2. TempData is a property of ControllerBase class.
  3. TempData is used to pass data from current request to subsequent request (means redirecting from one page to another).
  4. It’s life is very short and lies only till the target view is fully loaded.
  5. It’s required typecasting for getting data and check for null values to avoid error.
  6. 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

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

Comments

Popular posts from this blog

gcAllowVeryLargeObjects Element

There are numerous new features coming with .NET 4.5 and here, on this blog, you can find several posts about it. But the feature we are goint to talk about today is very exciting, because we were waiting for it more than 10 years. Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio 2012, and create C# Console Application, like picture below. First lets create simple struct with two double members like example below: 1 2 3 4 5 6 7 8 9 10 11 12 public struct ComplexNumber {      public double Re;      public double Im;      public ComplexNumber( double re, double im)      {          Re=re;          Im=im;      } } As we know this structure consumes about 16

Support for debugging lambda expressions with Visual Studio 2015

Anyone who uses LINQ (or lambdas in general) and the debugger will quickly discover the dreaded message “Expression cannot contain lambda expressions”. Lack of lambda support has been a limitation of the Visual Studio Debugger ever since Lambdas were added to C# and Visual Basic.  With visual studio 2015 Microsoft has added support for debugging lambda expressions. Let’s first look at an example, and then I’ll walk you through current limitations. Example To try this yourself, create a new C# Console app with this code: using System.Diagnostics; using System.Linq; class Program { static void Main() { float[] values = Enumerable.Range(0, 100).Select(i => (float)i / 10).ToArray(); Debugger.Break(); } } Then compile, start debugging, and add “values.Where(v => (int)v == 3).ToArray()” in the Watch window. You’ll be happy to see the same as what the screenshot above shows you. I am using Visual Studio 2015 Preview and it has some limitations.

How to allow a very large object in .net application?

Since .NET 1.0 the memory limit of .NET object is 2GB. This means you cannot for example create array which contains elements with more than 2GB in total. If try to create such array, you will get the OutOfMemoryException. Let’s see an example how to produce OutOfMemoryException. Before that Open Visual Studio, and create C# Console Application. Lets create simple struct with two double members like example below: public struct ComplexNumber { public double Re; public double Im; public ComplexNumber(double re, double im) { Re = re; Im = im; } } As we know this structure consumes about 16 bytes of memory. So if we want to create array of this type which consume more than 2GB we need to create array at least with 134217728 instances. So this sample program below creates 130000000 (about 1,97 GB) of array. int maxCount = 130000000; ComplexNumber[] arr = null; try { arr = new ComplexNumber[maxCount]; } catch (Exception ex) { Console.WriteLine(ex.Message); } So if we run t