Is using ViewBag in MVC bad? [closed]
Asked Answered
I

1

36

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?

Iconic answered 22/1, 2011 at 3:50 Comment(1)
Yes, 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).Lilli
D
44

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.

Diffract answered 22/1, 2011 at 4:1 Comment(10)
Then why have Microsoft created the ViewBag at all?Circumjacent
@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.Zared
I tend to say that this bring newbies to the world of "Bad practice".Iconic
Using ViewBag should be fine for setting things like the page title in a view, which will be passed to the master layout.Swartz
@Iconic 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.Bullnose
@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.Iconic
What 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?Kingfish
@Kingfish 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.Stanford
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...Alguire
If ViewBag is so bad why use it for Title even?Polymerism

© 2022 - 2024 — McMap. All rights reserved.