I am new to MVC. I want to know that when I should use ViewData, ViewBag and TempData to pass objects and is there any difference in performance of ViewData and ViewBag?
View Data: View Data is a dictionary object that is derived from View Data Dictionary class. View Data is a property of Controller Base class. View Data is used to pass data from controller to corresponding view. Usage:
public ActionResult Index()
{
ViewData.Name = "Tony Boss";
return View();
}
View Bag View Bag is a dynamic property that takes advantage of the new dynamic features in C# 4.0. Basically it is a wrapper around the View Data and also used to pass data from controller to corresponding view. View Bag is a property of Controller Base class.
Usage:
public ActionResult Index()
{
ViewBag.Name = "Tony Boss";
return View();
}
Temp Data: Temp Data is a dictionary object that is derived from Temp Data Dictionary class and stored in short lives session Temp Data is a property of Controller Base class. Temp Data is used to pass data from current request to subsequent request (means redirecting from one page to another).