View model is a class, that represents data model used in specific view.
View model has many roles:
- View models documents view by consisting only fields, that are represented in view.
- View models may contain specific validation rules using data annotations or IDataErrorInfo.
- View model defines how view should look (for
LabelFor
,EditorFor
,DisplayFor
helpers). - View models can combine values from different database entities.
- You can specify easily display templates for view models and reuse them in many places using DisplayFor or EditorFor helpers.