Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewModels or ViewBag?

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.

like image 889
Riddick Avatar asked Dec 08 '12 16:12

Riddick


People also ask

Which is better ViewBag or ViewData?

In theory if properly implemented, the ViewBag would ultimately outperform the use of the ViewData dictionary because the binding of the expressions (e.g. ViewBag.

Should I use Viewmodels?

The ViewModel is essential when you want a separation of concerns between your DomainModel (DataModel) and the rest of your code.

Is it good to use ViewBag?

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.

Why do we use Viewmodels?

The purpose of ViewModel is to encapsulate the data for a UI controller to let the data survive configuration changes. For information about how to load, persist, and manage data across configuration changes, see Saving UI States.


2 Answers

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.

like image 158
Darin Dimitrov Avatar answered Oct 12 '22 12:10

Darin Dimitrov


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?

like image 38
Tim Abell Avatar answered Oct 12 '22 13:10

Tim Abell