I consider a Viewmodel in itself, as a representation of a model to a view. I don’t see as a problem the return of ViewModel
from a Action
or Web Service
, since the information being returned is what you intend with this method.
The problem, in a case like this, is whether one of the cases needs a lot of information (example: many properties, for a detailed view of an entity) and the other with a few (example: a listing in the format of grid
), in such cases it is preferable to carry out the separation into different ViewModel
's, especially when working with distributed information (Web Services, Apis) where traffic is important. If the intention of the method is the same for both cases, I don’t see as a problem since you save code.
Viewmodel is not sent to the user in HTML format, this happens when your presentation layer renders HTML to the user. What is common is that in the body of requests to Apis or Web Services, this Viewmodel is serialized according to what was requested in the Header Request (content-type), and this format may vary according to the support of the API or Web Service. The most common are: json
, xml
and soap
.