Reputation: 15770
I have a problem i am trying to solve.
I have a grid (tabular) type layout that will contain a collection of ViewModels
.
I want to be able to validate those ViewModels
and then turn the cells for a given property red if it contains error:
Prop 1 | Prop 2 | Prop 3 | Prop 4
Row 1 x | | x |
Row 2 x | x | | x
Row 3 | x | x |
x = Red Cell
My question is how do I do this using model state that is filled from a service layer?
I am using AutoMapper to map Domain Objects (POCO) to ViewModels, where the POCO's are supplied by a service layer.
So basically:
Controller --> Service --> Returns Domain Object --> Maps to view models --> handed to view.
The business logic is in the service layer and I was thinking of passing in a model state wrapper to the service to fill the model state with errors from the domain objects.
I would assume some sort of key?
I know that model state is per property right?
Thanks!!
Upvotes: 0
Views: 906
Reputation: 1039438
It's not very clear how are you performing the validation at your service layer but at the end of the day the following items in your ModelState must have errors associated to them:
Items[0].Prop1
Items[0].Prop3
Items[1].Prop1
Items[1].Prop2
Items[1].Prop4
Items[2].Prop2
Items[2].Prop3
Where Items
is the name of the collection property on your view model. So you will have to add those errors to the ModelState with those keys.
Upvotes: 1