There are three options in Model View Controller (MVC) for passing data from controller to view. This article attempts to explain the differences among ViewData, ViewBag and TempData with examples. ViewData and ViewBag are similar and TempData performs additional responsibility. The following are the key points on those three objects.
ViewData
- The ViewData is used to move data from controller to view.
- The ViewData is a dictionary of objects that are derived from the "ViewDataDictionary" class and it will be accessible using strings as keys.
- ViewData contains a null value when redirection occurs.
- ViewData requires typecasting for complex data types.
ViewBag
- ViewBag is just a dynamic wrapper around ViewData and exists only in ASP.NET MVC 3. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
- ViewBag doesn't require typecasting for complex data types.
- ViewBag also contain a null value when redirection occurs.
TempData
- ViewData moves data from controller to view.
- Use TempData when you need data to be available for the next request, only. In the next request, it will be there but will be gone after that.
- TempData is used to pass data from the current request to the subsequent request, in other words in case of redirection. That means the value of TempData will not be null.
Now create a MVC Application to understand what the differences among ViewData, ViewBag and TempData are. The following example is for a Current Request.
Model Code
Right-click on "Model" and add a class with the name Customer.cs and add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication2.Models
{
public class Customer
{
public string UserName { get; set; }
}
}
Controller Code
Right-click on the "Controller" folder and add a controller with the name "Customer Controller" and add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class CustomerController : Controller
{
//
// GET: /Default/
public ActionResult Index()
{
var customerName = new Customer
{
UserName = "Rohatash",
};
ViewBag.Customer = customerName; // Defines ViewBag
ViewData["ViewCustomer"] = customerName; // Defines ViewData
TempData["TempCustomer"] = customerName; // Defines TempData
return View();
}
View Code
The is the View for showing the user's details. Right-click on "Controller method Index" and add View to the corresponding controller and add the following code:
@model MvcApplication2.Models.Customer
@{
var ViewBagTest = (ViewBag.Customer as MvcApplication2.Models.Customer).UserName;
var ViewDataTest = (ViewData["ViewCustomer"] as MvcApplication2.Models.Customer).UserName;
var TempDataTest = (TempData["TempCustomer"] as MvcApplication2.Models.Customer).UserName;
<div>
<h1>@ViewBagTest</h1>
<h2>@ViewDataTest</h2>
<h3>@TempDataTest</h3>
</div>
}
Now run the application and see the output. It will work fine.
Next Request (Redirection) Code
Now see the example of a Next Request to understand the TempData object. In the Next Request (Redirection), the TempData value does not become null. But in the case of ViewData and ViewBag the value becomes null. Now change the CustomerController code to make a redirection (Next Request).
Now the CustomerController Code is as the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication2.Models;
namespace MvcApplication2.Controllers
{
public class CustomerController : Controller
{
//
// GET: /Default/
public ActionResult Index()
{
var customerName = new Customer
{
UserName = "Rohatash",
};
ViewBag.Customer = customerName; // Defines ViewBag
ViewData["ViewCustomer"] = customerName; // Defines ViewData
TempData["TempCustomer"] = customerName; // Defines TempData
return RedirectToAction("TempDataTest");
}
public ActionResult TempDataTest()
{
String ViewBagTest = ViewBag.Customer; // After the redirect, ViewBag contains null value.
String ViewDataTest = (string)ViewData["ViewCustomer"]; // After the redirect, ViewData also contains null value.
String TempDataTest = TempData["TempCustomer"].ToString(); //After the redirect, Only TempData survives a redirect
return View("Index");
}
}
}
Now set a break point on the TempDataTest method and press F11 to check the next line's value.
Now select "ViewData.Customer" and right-click on it and select "QuickWatch".
Now click on the QuickWatch and see the value of ViewBag. It will show a null value.
Similarly, You can check the "ViewData" value that it also contains a null value.
If the ViewData or ViewBag value becomes null then a next request or redirection will take place. In the Next "Request( Redirection)", the TempData value does not become null. That means that after the redirect, only TempData survives a redirect. Now press F11 to see the value of TempData.
The Temp data of the current and subsequent request only means it is used when you are sure that the next request will be redirecting to the next view.