ViewModel in MVC

Monday, 22 April 2013

ViewModel in MVC


ViewModel is a complex object that may contain multiple entities or objects from different data models or data source.

ViewModel contain fields that are represented in the view (for LabelFor,EditorFor,DisplayFor helpers)
ViewModel can have specific validation rules using data annotations or IDataErrorInfo.
ViewModel can have multiple entities or objects from different data models or data source.

In ViewModel put only those fields/data that you want to display on the view/page.
Since view reperesents the properties of the ViewModel, hence it is easy for rendering and maintenance.
Use a mapper when ViewModel become more complex.

No comments:

Post a comment