This article is a case study which you may need to read a general information about MVC first. Click here for basics.
ViewData is simply a dictionary composed of key / value pairs, which will contains data to be passed between controller and views. Controller will add the items to this dictionary and view reads from it.
While ViewData is an easy to use collection of data it holds objects internally. When a new value is added into the ViewData, it gets boxed to object type. So unboxing is required every time we extract values out of it.
On the other hand, ViewBag is just the dynamic representation of ViewData. ViewBag uses the dynamic feature of C# 4.0 and makes ViewData dynamic. From the definition it is clear that ViewBag internally uses ViewData.
We can add data to ViewData and read it from the ViewBag and the Vice versa is also possible.
While ViewData and ViewBag is an easy way for passing values between Controller and Views it’s not a good practice to use any of them.
First of all, data stored as Object type in ViewData so we need to have conversions every time we want to use it. This is a performance issue and more than that, it may result in wrong type of conversion and open to runtime errors.
Another major disadvantage is, as the controller and view are loosely coupled they both are unaware about what’s happening inside of them so incorret values from the controller may be passed to the ViewData/ViewBag and therefore to the View leads into run time issues and inefficiency in development.