I would like to use the built-in validation features as far as possible. I would also like to use the same model for CRUD methods.
However, as a drop down list cannot be done using the standard pattern, I have to validate it manually. In the post back method, I would like to just validate the drop down list and add this result to ModelState so that I don’t have to validate all the other parameters which are done with Data Annotation. Is it possible to achieve this?
I may be mistaken about the drop down list, but from what I read, the Html object name for a drop down list cannot be the same as the property in the Model in order for the selected value to be set correctly. Is it still possible to use Data Annotation with this workaround?
Thanks.
You can use the
addModelErrorwhen you use that, it will invalidate the ModelState so
isValidwill return false.Update
after seeing the comment to @Pieter’s answer
If you want to exclude an element from affecting the
isValid()result, you can use theModelState.Remove(field)method before callingisValid().