Catchy title to this little post! If you are validating a radio button and the user has not selected any radio buttons in the group then you will get an error if you use ModelState.AddModelError(“radiobuttonname”,”my error message”) unless you explicitly use ModelState.SetModelValue and pass in a ValueProvierResult as the model binder doesn’t like the null when it binds the view.
ModelState.AddModelError(“gender”, “Please select male or female.”);
ValueProviderResult vpr = new ValueProviderResult(null, “”, null);
I have been writing a bunch of stuff in asp.net mvc of late and have been using linq to SQL to quickly pull together my DAL (although I have been using a repository pattern and castle Windsor to implement an IOC container which I picked up from the excellent pro asp.net mvc book). One of my entities is a profile object and, on editing, I only wanted to update the image field if the user uploaded a new image. After struggling to skip the field with linq I found I could select the old profile object from the data context and simply update the other fields declaratively if no Image is passed in the new profile object. I can’t help feeling there is a more elegant way of doing this but I have had to be pragmatic and implement this as it works. I’ll post the code shortly.