It seem like mvc 3 team decided to bring in a feature for dynamic data exchange between a controller and a view called the viewbag but it is A good thing against the strongly typed view we all know about? What are some of the positive and negative aspects to using the ViewBag
versus using a strongly typed view?
Asked
Active
Viewed 1.0k times
35
-
1Yes, the ViewBag is bad. Strong typing is a best practice for many reasons (which you can research for yourself). I would use the ViewBag very sparingly. The REAL problem with bad practices like these arises because people have a tendency to cut-and-paste code from project-to-project...so it ends up infecting your solutions like a disease. Why not take the time to write good code? Then, if development speed is the issue, share that code using an internal NuGet server (instead). – Prisoner ZERO Sep 30 '15 at 18:14
1 Answers
43
The ViewBag is the same thing as ViewData in previous ASP.NET MVC 1 and 2. It just happens to be dynamic instead of needing to use it like a dictionary with keys. I don't think this will replace strongly typed views at all and in fact you should use Viewdata/Viewbag as little as possible. Always use strongly typed views whenever possible since it will lead to fewer errors if the names in your Viewdata/Viewbag change and make the HTML cleaner by not having ViewData casts all over the place.

amurra
- 15,221
- 4
- 70
- 87
-
5
-
3@CarstenGehling it's there so you have the option to do things in a quick and dirty manner if you choose. Sometimes business needs a quick and dirty solution, and MVC doesn't try to make that call for you. It's up to you. – Christopher Nov 10 '11 at 15:59
-
12
-
1Using ViewBag should be fine for setting things like the page title in a view, which will be passed to the master layout. – Fred Wilson Dec 27 '11 at 18:35
-
@Rushino That's one way to look at it. Bear in mind that there are other, more dangerous, ways to abuse the API. Not just MVC, but .NET in general. The best way to deal with it is education. Learn the right way to do things and when you pass on your knowledge (through training junior devs or pair programming) - teach them the right way. – Arnold Zokas Feb 01 '12 at 01:35
-
@FredWilson I agree with you. The ViewBag seem good for something such page title. Why not ? Would be a bit overkill to set it in the viewmodel. – Rushino Feb 01 '12 at 02:04
-
4What about using the viewbag to hold a collection of items to populate a drop down? I don't really want to create that collection in my view model as my view model only cares about what was selected. Thoughts? – Boone Sep 13 '12 at 15:29
-
3@Boone I always have the collection of items as part of the viewmodel class. It just makes more sense to me. Also I can write various unit tests targeting that collection. The only time I use ViewBag is `ViewBag.Title` in my views and that's it. I can count on one hand the number of times I've typed the word ViewBag in any MVC project I've worked on. – Jason Evans Jun 18 '13 at 12:09
-
this is wrong .. your strongly typed view is also part of ViewData... a viewdata has model property which allows to store strongly typed in viewdata which is same place your other data lies except its in bag not on some model kind of property... – Vishal Sharma Nov 19 '13 at 08:32
-