tags:

views:

48

answers:

1

I'm a bit new to ASP .NET MVC and I'm having trouble understanding the need for ViewModels, and what makes them different from regular Models. Any good resources on the topic ?

+2  A: 

They are strongly tied to the view. For example your Model might contain many properties but you only need to manipulate a subset of them on a given view. In this case you would create a ViewModel to represent the properties you would like to work with on a given view. So you could have multiple ViewModels for a given Model because this Model might have many representations. A ViewModel might also contain formatted data which is more suitable to be shown on a view, while the Model contains raw data (for example DateTime formatting, currencies, etc...).

Everytime you write a single line of C#/VB.NET code in a view it means that this is a good candidate for a view model and/or html helper.

Darin Dimitrov
So the importance of the ViewModel is its tie to the view, not necessarily from any functionality it provides ?
JHarnach
Its functionality is to make the data more adapted and easily rendered by the view. Also you might be aggregating data from many different data sources, so you would have different models, but on the view you will display properties from those different source, so you create a viewmodel to contain everything you need to display.
Darin Dimitrov