Various Methods of Data Transmission from Controllers to Views
There are several techniques to transfer data or value from the controller to the view.
ViewData Dictionary Property
By adding objects to the ViewData dictionary using a key/value pattern, you can supply data that you want to give to a view.
ViewBag Property
In ASP.NET MVC, temporary data (that isn't part of the model) is transferred from the controller to the view via the ViewBag.
It is a dynamic type property of the ControllerBase class, which serves as the Controller class's base class, on the inside.
Used to pass data from controller to view.
- It is derived from ViewDataDictionary class.
- It is available for the current request only.
- Requires typecasting for complex data types and checks for null values to avoid an error.
- If redirection occurs, then its value becomes null.
Used to pass data from the controller to the respective view, using a dynamic property.
- It is also available for the current request only.
- If redirection occurs, then its value becomes null.
- It doesn’t require typecasting for the complex data type.
- Keeps its information for the time of an HTTP Request i.e. request from one page to another.
- Requires typecasting for complex data types and checks for null values to avoid an error.
- In General, it is used to store only one time messages like the error messages and validation messages