DataAnnotationsModelBinder with MVC2 RTM
Trying to validate models with Da开发者_JAVA百科taAnnotations but DefaulModelBinder overrides my Required property error messages and never uses my error messages for invalid data entry. Always show 'value' is invalid for 'property name'.
In another question I saw that MVC 2 uses DataAnnotationsModelBinder but I couldn't find any class in MVC 2 binaries. I downloaded the source for MVC futures and changed some source to compile it for .Net 4.0 but although I had success to compile, it has compatability problems and doesn't work as expected.
Any help is aprreciated.
Do not try to use the DataAnnotationsModelBinder with MVC 2 RTM. MVC 2 RTM's DefaultModelBinder already contains all of the logic that was present in the DataAnnotationsModelBinder sample.
This is happening because an exception is thrown in the DefaultModelBinder
when it tries to cast the input value to the type of the receiving model property.
You can either use string
as type for the properties on the receiving model
or make a custom Model binder by implementing IModelBinder
and handle the validation.
精彩评论