I have a site that uses Entity Framework for data persistence.
I have used the buddy class system on a number of Entities to flag what fields are required using data annotations.
ASP.NET MVC and its ModelState work great with this out of the box.
My question is, I have some validation that is a little more involved –
Is there a way I can include this business logic in the buddy class in such a way that the ModelState is populated correctly?
An example of this would be
If field X is of one value, then Field Y is required.
The logic may get more complex than this.
Thanks,
You should make the entities themselves implement
IValidatableObject, then use an iterator toyield returnaValidationResultfor each error.Alternatively, you can make a custom class-level
ValidationAttributeand apply it to the buddy class.