ASP.Net MVC - ViewBag, ViewData and Tempdata

Datetime:2016-08-23 04:37:20          Topic: ASP.NET MVC           Share

As an ASP.Net MVC developer, you may frequently involved with these terms like Tempdata, viewdata and viewbag. In this post, I’m going to discuss about what are they and what are the main difference between them.

ViewBag:

  1. ViewBag is dynamic in nature.
  2. There is no type casting needed.
  3. Life time lasts only for the current request.

View Data:

  1. ViewData is stored as a dictionary. You can store a value using a key.
  2. Type casting is needed, when you retrieve.
  3. Life time lasts only for the current request.

Tempdata:

  1. It’s also store the value using key.
  2. Need type casting when the stored value retrieved.
  3. The life time of tempdata is little bit tricky. It will be available till you read it first time. After you read it, it will be marked for deletion. So in the next request, it won’t available.  However, you can extend it’s life time by using keep and peek method.

Hope this helps!





About List