I have a “list” view which is basically receiving a model of type IEnumerable(Thing). I have no control over Thing; it is external.
That’s been nearly fine. One of the properties of ‘Thing’ is an enum of flags. I’d like to improve the formatting of this property in the view. I have read some strategies for that. My plan was to create a ViewModel that knows better about formatting.
I’d like to know if there’s a direct way to create an IEnumerable(ViewThing) from IEnumerable(Thing).
An obvious way would be to iterate through the IEnumerable of Things where for each Thing I would create a ViewThing and stuff it with the Thing’s data, resulting in an IEnumerable of ViewThings.
But backing up, I’m also interested in smarter ways to handle formatting the flags for viewing.
You could use AutoMapper to map between your domain models and view models. The idea is that you define a mapping between
ThingandThingViewModeland then AutoMapper will take care of mapping collections of those objects so that you don’t have to iterate:Now all that’s left is define the mapping between a
ThingandThingViewModel: