Jul 14, 2011 06:14 AM|francesco abbruzzese|LINK
pls Note the following:
1) This is a problem of just metadataprovider, in you case the problem is with the Display attribute
2) Validation attributes are handled by the Validation provider, that has no problem if some property is missing.
3) The [required] attribute is checked by the model binder , and if the ViewModel has no matching property a validation error is raised(no exception...it behave just if the user misses the filed)
4) All validation attributes but the required attribute works fine also if the ViewModel has missing properties
5) Removing this behaviour from the required attribute create a security problem (that is why this behaviour was inserted). Moreover, the fact that an attribute is required in the model doesn't imply it is required in the ViewModel since the value of the
required fileld might come from a source different from the user, for instance another db table...or by looking at the logged user. I suggest to use a third Metadata class to be used as metaclass for both the model and the ViewModel, and to avoid putting
the required attribute in this metacdata class(just put them directly in the model or in the viewmodel as needed). For more infos, pls read my blog post: http://www.dotnet-programming.com/post/2011/04/13/Data-Dictionary-And-Data-Annotations.aspx
Summing up the problem is just with the data provided by the MetaData provider, not with the validation attributes.
Now why don't you simply write your Metadataprovider, reading yourself the attributes with the help of reflection, and then set your provider as the default one? You are not forced to use the function inherited by the base class to retrieve the metadata...you
can do it with your functions. You need just to use a little reflection.
Just download the sources and mofify them.