ViewData in MVC

  1. ViewData in MVC is a public dictionary object, which is used to pass data from controller’s action to corresponding view. This object is derived from ViewDataDictionary class.
  2. The data stored in this object will be available during the current request only. And this object will be set to null, when redirection occurs. So we can’t retrieve the data stored in this object after redirection.
  3. This object is like a temporary session variable but its life time is very short. As I told earlier, the life of this object will end, when redirection happens.
  4. And type casting is required, when we use this object for data in complex data type and check for null before use is the best practice to avoid errors like “object reference not set to an instance of an object”.
For more detail and source code, ViewData in MVC.
Ebook Download
View all
Learn
View all