Search code examples
asp.net-mvcviewmodel

Does a ViewModel have its own logic?


When assigning ViewModel fields, should the domain objects be passed directly to the ViewModel objects which will then determine how to present the data, or should another class like a service be assigning data from the Model to the ViewModel?

also:

EDIT:is there any sense in dividing a viewmodel into receiver and presenter? (instead of binding only certain fields on update?)


Solution

  • Usually the Controller Action takes the business objects and puts whatever is needed by the viewmodel.

    If you have a business object that contains the fields Name, Address, Id and the View should only display the Name, then the ViewModel only has a field "Name", and the controller action populates it. The ViewModel should know nothing about your Business Classes, it should only know about the stuff that it needs to display.

    The main/only logic is then "Display Logic", that is stuff like "if TotalAmount is negative, display it with CSS Class negativeNumber".