ViewData, ViewBag & TempData all these are state management techniques in ASP.NET MVC.
ViewData & ViewBag are quite similar. Both are used to transfer information from the controller's action to view. But below are some key differences between them.
Syntax for ViewData:
ViewData["Email"] = "email@example.com";
Syntax for ViewBag:
ViewBag.Email = "firstname.lastname@example.org";
TempData is a bit different, this is used to transfer information from one controller action to another controller action during full page redirects. TempData uses session behind the scenes.
Syntax for TempData:
TempData["Email"] = "email@example.com";
ViewData & ViewBag both are the properties of
ControllerBaseclass. Type of ViewData is
ViewDataDictionarywhile type of ViewBag is
dynamic. Visit ControllerBase class here.