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"] = "ankushjain358@gmail.com";
Syntax for ViewBag:
ViewBag.Email = "ankushjain358@gmail.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"] = "ankushjain358@gmail.com";
ViewData & ViewBag both are the properties of
ControllerBaseclass. Type of ViewData isViewDataDictionarywhile type of ViewBag isdynamic. Visit ControllerBase class here.