0

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!!

Sam
  • 15,336
  • 25
  • 85
  • 148
  • can you explain what you mean by "using model state that is filled from a service layer"? Are you using a specific language/library? – loveToCode Oct 11 '12 at 01:51
  • It might help if you were a little more specific. "objects" is what? – Erik Funkenbusch Oct 11 '12 at 01:51
  • Couldn't you just add a property to your ViewModel `IsValid`, do the validation in the service and return the collection to the view? – lukiffer Oct 11 '12 at 02:04
  • @lukiffer - The requirements are that the cell of the table needs to be red if the property is not valid. So you have to be able to track each property for each view model in the collection. – Sam Oct 11 '12 at 02:07
  • 1
    What are you using for validation? Built-in validation like dynamic data or custom business logic validation? – lukiffer Oct 11 '12 at 02:11
  • @lukiffer - Custom business logic. – Sam Oct 11 '12 at 05:04

1 Answers1

1

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.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928