Difference between ViewBag & ViewData
Different between viewbag & viewdata.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Munesh SharmaPosted May 2, 2016, 2:35 PM
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.
Ravi PatelPosted Apr 19, 2016, 1:18 AM
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
Vivek KumarPosted Apr 19, 2016, 12:29 AM
Ashish SrivastavaPosted Apr 18, 2016, 3:32 PM