So lets say I have two models:
Thingy and Status. Thingy has a Status, and Status has many Thingies. It’s typical “Object and Object type relationship”.
I have a view where I just want the number of thingies in each status. Or basically a list of Status.Name and Status.Thingies.Count. I could do exactly this, but is the “right” thing to do to create a view model in the form:
ThingiesByStatusViewModel
-StatusName
-StatusThingiesCount
and hook it up with something like AutoMapper.
For such a trivial example, it probably doesn’t make much of a difference, but it would help me understand better the proper ‘separation of concerns’.
Is this a rhetorical question?
Your view model would look exactly as you propose and it is perfectly adapted to what you are trying to display here:
and then your controller would return an
IEnumerable<ThingiesByStatusViewModel>. Then in your view you could simply use a display template:and the corresponding display template (
~/Views/Shared/DisplayTemplates/ThingiesByStatusViewModel.cshtml):Now let’s look at the mapping layer. Suppose that we have the following domain:
and we have an instance of
IEnumerable<Status>.The mapping definition could look like this:
and finally the controller action would simply be: