ViewModels or ViewBag?
Asked Answered
G

2

39

I'm fairly new to MVC4, EF5 and ASP.Net, and I don't seem to be able to find a good answer anywhere.

Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag?

Say I have a method which populates a drop down list, and I am using a viewmodel to represent the output for the view.

Am I ok to use Viewbag.DropDown = PopulateDropdown(); or would it be better to incorporate this into the ViewModel, by creating a property to hold the List<SelectListItem> created by PopulateDropdown(); ?

I know how handy ViewBag is, but I'm yet to see any solid reason as to not use it? If anyone could also offer me some more insight, that would be fantastic.

Galimatias answered 8/12, 2012 at 16:18 Comment(0)
P
55

Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag?

Everything should be done inside a view model. That's what a view model is. A class that you specifically define to meet the requirements of your view. Don't mix ViewBags with ViewModels. It is no longer clear for the view where is the information coming from. Either use only a view model (approach that I recommend) or only use ViewBags. But don't mix the 2.

So in your particular example you would have a property on your view model which is of type IENumerable<SelectListItem> and inside your view you will use the strongly typed version of the Html.DropDownListFor helper to bind to the model:

@Html.DropDownListFor(x => x.ProductId, Model.Products)

Obviously those are only my 2 cents. Other people will say that mixing ViewModels and ViewBags is fine and I respect their opinion.

Patmos answered 8/12, 2012 at 16:20 Comment(5)
Thanks, Is there any other benefit to using ViewModels over ViewBag apart from the abstraction it offers?Galimatias
Of course that there is. You get Intellisense and you can use the strongly typed versions of the Html helpers inside your views. You also get a refactor friendly code and no longer rely on magic strings. Also it is clear where the information is coming from to a given view by only looking at the view model that this view is strongly typed to. If you were using a mixture of ViewModels and ViewBag you should also look at the controller action which is setting the ViewBag.Patmos
also you can use data annotations and make controllers receive your viewmodels. like: public RedirectToRouteResult Add(int id, SomeObjectViewModel someObject) {...}Shoifet
How to add the default value?Rockbound
I'd disagree that it's the mixing of the two that's the problem per se. It might be reasonable to pull in your dropdown data with a viewbag whilst using a view model for the rest of a form's data. Having said that, I dislike the use of viewbag due to the lack of compile time checking, intellisense and IDE support (navigate to definition, find usages etc). I think you could make it clearer in your answer that this is a big downside to viewbags.Legend
L
24

Prefer ViewModels over the ViewBag wherever you can. Create Strongly typed views. It makes your code cleaner, less fragile, less error-prone, and easy to maintain.

ViewBags are just dictionaries of dynamically typed objects so you lose:

  • Compile time checking
  • The ability to refactor with confidence (you lose the support of the tools)
  • IDE support - such as the ability to navigate to all usages
  • Intellisense

For bonus points making extensive use of the ViewBag also misses the point of using the MVC pattern

I get the impression ViewBags were created to solve an edge-case problem in asp.net and people use them instead of creating view models as was originally intended in the design of the platform, to the detriment of their work.


with thanks to Why not to use ViewBag heavily?

Legend answered 18/5, 2016 at 10:30 Comment(3)
I keep seeing this thing about intellisense. It is not an issue; you simply declare/ convert it in the view and you have intellisense. I admit I don't use viewmodels (possibly due to laziness) but it seems to be no hassle to use a viewbag considering that it is easy to get intellisense for it.Rehearing
If you declare it in the view you have intellisense in the same way as if you made a viewmodel. The viewbag type is whatever it was given - you know what object you have given it so you make the same object in the view. If you have a list of objects for example you declare that list in the view near the top of the page and send the viewbag to it. I can do a viewmodel or viewbag - neither one is much better to look at than the other. Intellisense is certainly never a problem unless you use the viewbag directly on the view.Rehearing
I was not looking for help - just stating that ViewBags can be configured to use intellisense. I am moving more towards using viewmodels now. I just came across this page when considering/ researching whether to use viewmodels more.Rehearing

© 2022 - 2024 — McMap. All rights reserved.