SiteMap  | About    

What is ViewData and ViewBag? MVC introduced ViewData and ViewBag to pass data between controller to view. Both are used for the same purpose. 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. Internally ViewBag properties are stored as name/value pairs in the ViewData dictionary.

Both ViewData or ViewBag valid only for the duration of the current request . If you set it in a controller action and use it in the view, then it disappears. The difference is that the ViewData is a dictionary whereas the ViewBag is just a dynamic wrapper around this dictionary. Both point to the same data though. Moreover, ViewData is required type casting for complex data type and check for null values to avoid error while ViewBag doesn’t require type casting for complex data type. (C) 2017    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.