Difference between ViewBag & ViewData
Different between viewbag & viewdata.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Ashish SrivastavaRank #2,169 · 655 reputationPosted Apr 18, 2016, 11:32 AM EDT
Vivek KumarRank #272 · 9.3K reputationPosted Apr 18, 2016, 8:29 PM EDT
Ravi PatelRank #439 · 5.7K reputationPosted Apr 18, 2016, 9:18 PM EDT
ViewData
ViewBag
It is Key-Value Dictionary collection
It is a type object
ViewData is a dictionary object and it is property of ControllerBase class
ViewBag is Dynamic property of ControllerBase class.
ViewData is Faster than ViewBag
ViewBag is slower than ViewData
ViewData is introduced in MVC 1.0 and available in MVC 1.0 and above
ViewBag is introduced in MVC 3.0 and available in MVC 3.0 and above
ViewData is also work with .net framework 3.5 and above
ViewBag is only work with .net framework 4.0 and above
Type Conversion code is required while enumerating
In depth, ViewBag is used dynamic, so there is no need to type conversion while enumerating.
It value become null if redirection is occurred.
Same as ViewData
It lies only during the current request.
Same as ViewData
Munesh SharmaRank #174 · 13.9K reputationPosted May 2, 2016, 10:35 AM EDT
ViewData
ViewData is a dictionary object that is derived from ViewDataDictionary class.
ViewData is a property of ControllerBase class.
ViewData is used to pass data from controller to corresponding view.
It’s life lies only during the current request.
If redirection occurs then it’s value becomes null.
It’s required typecasting for getting data and check for null values to avoid error.
ViewBag
ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
Basically it is a wrapper around the ViewData and also used to pass data from controller to corresponding view.
ViewBag is a property of ControllerBase class.
It’s life also lies only during the current request.
If redirection occurs then it’s value becomes null.
It doesn’t required typecasting for getting data.