Thursday, 27 December 2012

Will ModelState.IsValid consider properties with RemoteAttribute data annotations?

Yes and... No.

The default model binder will call the IsValid() method of the RemoteAttribute class.

However, this method simply returns the value 'true'.

This is why it seems that the Model validation is simply ignoring any RemoteAttribute properties on your Models...

I personally was hoping that the IsValid() method of the RemoteAttribute class would be clever enough to call the method specified in the HttpMethod property which handles the validation.

To make this happen, there are a couple of options I have found:

1. Make your model implement the IValidatableObject interface. Information about this can be found in this article:
Exercise 3: Using IValidatableObject Custom Validation
and on this StackOverflow post:
ModelState.IsValid vs IValidateableObject in MVC3

2. Create a custom validation attribute which inherits from the RemoteAttribute and override the IsValid() method to call the correct validation procedure. Information on this technique can be found in my post here:
Creating Custom Remote Attributes

No comments:

Post a Comment