In ASP.NET MVC there are three ways to pass/store data between the controllers and views.
ViewData
- ViewData is used to pass data from a controller to view.
- It is derived from ViewDataDictionary class.
- It is available for the current request only.
- Requires typecasting for complex data type and checks for null values to avoid error.
- If redirection occurs, then its value becomes null.
ViewBag
- ViewBag is also used to pass data from the controller to the respective view.
- ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
- It is also available for the current request only.
- If redirection occurs, then its value becomes null.
- Doesn’t require typecasting for the complex data type.