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 ?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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.