I was reading Rachel Appel’s post today on when to use ViewData, ViewBag, and TempData and I think it’s a good read. I left a comment but it disappeared… I think her comments might be borked… so I’ll add it here as I think it’s valuable.
Here’s the comment I left:
———————————————————————————-
Hi Rachel - nice post :). I think there’s a lot of confusion about this and it’s nice to see you tackle it :).
If I may - it might be worth pointing out that *everything* (aside from TempData) gets popped on ViewData, which in turn is popped onto ViewContext - the only question is “how much misdirection do you want”.
I think what you’re alluding to (correct me if I’m wrong) is handling more complex data structures with a typed class rather than pushing simple key-value bits using ViewData - which is a good approach.
However it’s also not exclusive. You can use a ViewModel *with* ViewBag and ViewData:
ViewBag.MyViewModel = new MyViewModel();
Since ViewBag is a wrapper for ViewData (as you mention) - it all cycles down to ViewData as a transport mechanism which is used no matter what you do.
Some people actually prefer this loose association between Controller/View :). For me - I tend to use Dynamic with everything so it doesn’t matter :).
Thanks again for writing this post :).
—————————————————————————————-
As an aside, for those who are curious - here’s the MVC Source code (from Controller.cs) that shows the assignment of the model data onto the ViewData bits…

My name is Rob Conery and I am the owner/smooth operator of Tekpub, creator of
This Developer's Life, and an avid Ruby/Rails/.NET developer.