I'm having an annoying issue with the RadioButton helper and Data Annotation used for validation:
I'm actually replacing a dropdown list with radio buttons (because the list is small). I'm not using MVC 2 or MVC Futures (though, I think I'd run into the same problem if I were using the RadioButtonList).
That particular field is required, hence:
[Required(ErrorMessage="A category is required")]
in my Model.
The field name is: category_id.
The problem I'm running into is that since no option is selected the FormCollection (assume it's "collection") doesn't have an entry for collection["category_id"]
.
Therefore, it doesn't try to bind that in my call to TryUpdateModel(). So, all my Data Annotation is totally ignored.
I'm having to check manually do a check for collection["category_id"]
in the controller code and manually all the error to the model (defeats the purpose of DataAnnotation).
Anyone got some bright ideas. I've been pulling my hair out for a couple hours now.
Is there a way to force the RadioButton to submit a collection["category_id"] = "" or null
when no item is selected (hence, triggering my binding logic)?