I consume an API that has a full object set. I don’t want to have to re-create an object model in the mvc3 code just to use the DataAnnotations.
Is there a way to use this feature with out a wrapper class? The objects have their own validation rules that I can reuse, but want to use the built in mvc framework to display the messages back.
You can create a class which inherits from
DataAnnotationsModelMetadataProvider, where you can consume your object set, and write out the items asDataAnnotations. A brief example:Then, register your provider in Global.asax.cs in the
Application_Startmethod, as follows: