Profile photo
Submitted by RaKumar
on October 4th, 2012 at 10:29:12 EST

ViewBag vs ViewData in MVC 3

What is the difference in using ViewBag vs ViewData in MVC 3 for passing data between Controller and View? Is there any benefits using one vs other?

Profile photo
Replied by SBajra
on October 4th, 2012 at 13:18:30 EST
Internally ViewBag uses ViewData. ViewBag is just a wrapper for ViewData to access data with properties like style using "dynamic" type.

For example, with ViewBag, you can access your value like:

This is done using "dynamic" type

whereas in ViewData, it will be accessed using magic string like:

ViewData is of type ViewDataDictionary which is collection of name/value pair.
Accepted Answer