3

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 ?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

So the importance of the ViewModel is its tie to the view, not necessarily from any functionality it provides ?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.