Wednesday, February 25, 2015

What is ViewBag, ViewData and TempData in MVC ?

In ASP.NET MVC, Generally we have three options for passing data from Controller to View and in the request. 

And that three options are :
1. ViewBag
2. ViewData and
3. TempData

ViewBag and ViewData are basically same and TempData have some additional responsibilities to perform.

ViewBag and ViewData
ViewBag and ViewData are generally used for same purpose. Both are used to transfer the data from controller to view and both of having a short life which lies only in current request. Short life means the value becomes null during the next request or any redirection occurs.

ViewBag is a dynamic property which is able to set and get value dynamically.
ViewBag is basically a wrapper around the ViewData.

ViewData is dictionary of an object which is accessible by string as a "key".
ViewData is a property of Controller that exposes an instance of ViewDataDictionary.

Difference between ViewBag and ViewData

ViewBag ViewData
ViewBag is a dynamic property
ViewData is a dictionary of object which if accessible using string as a "key".
ViewBag is a new dynamic feature of C# 4.0
ViewData is derived from ViewDataDictionary class.
Works with .Net framework 4.0
Works with .Net framework 3.5
It doesn't require typecasting for complex data type.
It requires typecasting for complex data type.

Example of ViewBag

At Controller

Public ActionResult Index()
    ViewBag.Author = "Nikhil";
    Return View();

At View
Example of ViewData

At Controller
Public ActionResult Index()
    ViewData["Author"]= "Nikhil";
    Return View();
At View

<label>@ViewData["Author"] </label>


TempData is also a dictionary which is derived from TempDataDictionary class.
TempData is stored data just like a session for limited period of time.
TempData holds the data for the time of HTTPRequest means it holds the data between two consecutive request.
So, the TempData is useful for transferring the data from one Controller to another Controller and one Action to another Actions.
Initially the TempData uses the session variables. It helps to maintain the data between controllers and actions. It requires the typecasting for such complex data types.
TempData is generally used to save one time message. but you can keep the value after the completion of request by using  keep()  method.

Example of TempData

Public ActionResult Index()
   var dbModelData = new Author (){ Name = "Nikhil", UserName = "DotNetter"};
   TempData["objModel"] = dbModelData;
   Return RedirectToAction("Login");
Public ActionResult Index()
   var modelDetails = TempData["objModel"];
   Return View(modelDetails);
At the End :

In ASP.NET MVC, we have three options ViewBag, ViewData and TempData for sending the data between Controllers and Views and in next request. Basically ViewBag and ViewData are same and used for sending data from Controller to View and TempData is used during current and subsequent request that means you can pass data between Controllers and Actions with the help of TempData.


Post a Comment