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"] = "firstname.lastname@example.org";
Syntax for ViewBag:
ViewBag.Email = "email@example.com";
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"] = "firstname.lastname@example.org";
ViewData & ViewBag both are the properties of
ControllerBaseclass. Type of ViewData is
ViewDataDictionarywhile type of ViewBag is
dynamic. Visit ControllerBase class here.