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

ViewBag

TempData

Now create an MVC Application to understand the differences between ViewData, ViewBag, and TempData. 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 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

This is the View for showing the user's details. Right-click on "Controller method Index" 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.

Output

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 follows.

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 breakpoint on the TempDataTest method and press F11 to check the next line's value.

TempData

Now select "ViewData.Customer" Right-click on it and select "QuickWatch".

Quick Watch

Now click on the QuickWatch and see the value of ViewBag. It will show a null value.

ViewBag

Similarly, You can check the "ViewData" value which also contains a null value.

ViewData

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.

Request

The Temp data of the current and subsequent request only means it is used when you are sure that the next request will be redirected to the next view.