ViewData, ViewBag, TempData What are ViewData, ViewBag, and TempData?


ViewData is the mechanism for a controller to provide data to the view it presents, without using a ViewModel. Specifically, ViewData is a dictionary which is available both in MVC action methods and views. You may use ViewData to transfer some data from your action method to the view returned by the action method.

Since it is a dictionary, you can use the dictionary like syntax to set and get data from it.

ViewData[key] = value; // In the action method in the controller

For example, If you want to pass a string message from your Index action method to your Index view Index.cshtml, you can do this.

public ActionResult Index()
   ViewData["Message"] = "Welcome to ASP.NET MVC";
   return View(); // notice the absence of a view model

To access this in your Index.cshtml view, you can simply access the ViewData dictionary with the key used in the action method.


ViewBag is the dynamic equivalent of the untyped ViewData dictionary. It takes advantage of the C# dynamic type for syntactical sugar experience.

The syntax for setting some data to ViewBag is

ViewBag.Key = Value;

So if we want to pass the message string in our previous example using ViewBag, it will be

public ActionResult Index()
   ViewBag.Message = "Welcome to ASP.NET MVC";
   return View(); // not the absence of a view model

and in your Index view,


Data is not shared between the ViewBag and the ViewData. ViewData requires typecasting for getting data from complex data types and check for null values to avoid error where as View Bag does not require typecasting.

TempData can be used when you want to persist data between one http request and the next HTTP request only. The life of data stored in the TempDataDictionary ends after the second request. So TempData is useful in scenarios where you are following the PRG pattern.

public ActionResult Create(string name)
   // Create a user
   // Let's redirect (P-R-G Pattern)
   TempData["Message"] = "User created successfully";
   return RedirectToAction("Index");
public ActionResult Index()
  var messageFromPreviousCall = TempData["Message"] as String;
  // do something with this message
  // to do : Return something

When we do return RedirectToAction("SomeActionMethod"), the server will send a 302 response to the client(browser) with location header value set to the URL to "SomeActionMethod" and browser will make a totally new request to that. ViewBag / ViewData won't work in that case to share some data between these 2 calls. You need to use TempData in such scenarios.