In several sample projects, I saw that ViewModels are used to convert data objects to strings, for use in a view.
ViewModel usually has a constructor that takes one parameter - a data object. Then the constructor will populate the various ViewModel properties (mostly strings and ints).
This prevents any complex logic from appearing in the view.
At first glance, this seems to me to be a good idea, since it more fully ensures the separation of representation from complex logic.
For example, let's say my view tried to display the “Size” property of a data object, the size of which was a number from 1 to 3, representing “Small / Medium / Large”.
Instead of having an if / switch statement in my view, I would just have “SizeString” or something similar in my ViewModel, and the if / switch statement went into the ViewModel constructor.
Does anyone disagree with this approach?
Would it be better to use some other approach, like helpers? And if so, why?
asp.net-mvc mvvm separation-of-concerns
Jonathan
source share