ASP.NET MVC offers us three options ViewData, VieBag and TempData for passing data from controller to view and in next request. ViewData and ViewBag are almost similar and TempData performs additional responsibility. Lets discuss or get key points on those three objects:
Similarities between ViewBag & ViewData :
*Helps to maintain data when you move from controller to view.
*Used to pass data from controller to corresponding view.
*Short life means value becomes null when redirection occurs. This is because their goal is to provide a way to communicate between controllers and views. It’s a communication mechanism within the server call.
Difference between ViewBag & ViewData:
*ViewData is a dictionary of objects that is derived from ViewDataDictionary class and accessible using strings as keys.
*ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
*ViewData requires typecasting for complex data type and check for null values to avoid error.