How would you validate a checkbox in ASP.Net MVC 2?
Posted
by Scott Mayfield
on Stack Overflow
See other posts from Stack Overflow
or by Scott Mayfield
Published on 2010-05-04T16:08:28Z
Indexed on
2010/05/04
16:18 UTC
Read the original article
Hit count: 706
Using MVC2, I have a simple ViewModel that contains a bool field that is rendered on the view as a checkbox. I would like to validate that the user checked the box. The [Required] attribute on my ViewModel doesn't seem to do the trick. I believe this is because the unchecked checkbox form field is not actually transmitted back during the POST, and therefore the validation doesn't run on it.
Is there a standard way to handle checkbox "required" validation in MVC2? or do I have to write a custom validator for it? I suspect the custom validator won't get executed either for the reason mentioned above. Am I stuck checking for it explicitly in my controller? That seems messy...
Any guidance would be appreciated.
Scott
© Stack Overflow or respective owner