ViewData, ViewBag And TempData In ASP.NET

We use these three variables  for passing values from controllers to view.


It is an object type and it is also known as a dynamic property object. ViewBag can get and set value dynamically. It does not require type conversion but converts type dynamically.


ViewData is similar to ViewBag., which transfers data from controller to view. ViewData is dictionary objects derived from ViewDataDictionary class. It is used to store and pass values. ViewData requires type-casting while accessing the data in the view. ViewData contains Key-value pairs where each key must be a string.


The value of TempData persists only from one request to the next. It is also used to pass data from one action to another action. TempData stores the data temporarily and automatically removes it after retrieving a value.

Next Recommended Reading ViewData VS ViewBag Vs TempData in MVC