xVal, MVC2 and radio buttons

Oct 22, 2009 at 8:03 AM

New to xVal and MVC, so please forgive me if this is a simple problem.

Given a domain model class with DataAnnotation attributes - let's just say Required for now - I have no problem using xVal validation under its default settings to validate things such as dropdowns, textboxes, etc on both the client and server side.

To simplify the discussion, please assume that my model object has a String property named "MyGroup", and that it is marked as required.  If I throw a text field on the page, no problem - everything binds as I would expect.  However, assume for a minute I have a group of radio buttons like this:

<input type="radio" name="MyGroup" value="A1" />A1<br/>
<input type="radio" name="MyGroup" value="A2" />A2<br/>
<input type="radio" name="MyGroup" value="A3" />A3<br/>
<input type="radio" name="MyGroup" value="A4" />A4<br/>
<input type="radio" name="MyGroup" value="A5" />A5<br/>

Using ASP.NET MVC2, VS2010 Ultimate Beta 2 and xVal, I cannot get the validation to fire for this!  (Yes, I realize a radio group for a string property is... odd.  But I'll hold off on my enum question for later.  :-) ).  I have seen the jQuery Validation demo with radio groups, and if I add this to my document.ready function:



Then I get a different effect - the validation "works", although it's no longer using the typical CSS, doesn't use my validationSummary client-side, etc (presumably because with that call I have bypassed xVal??  Really not sure, I'm still very new to this).

Can someone give me a working example for how you take a radio group, using ASP.NET MVC 2 and xVal, and validate that a selection has been provided?



Oct 30, 2009 at 10:39 AM

I have a similar problem and I would like to add to your description.

When I disable xVal

  • on client side by commenting out Html.ClientSideValidation
  • server side by commenting out DataAnnotationsValidationRunner.GetErrors

, the server validations contnue to work because of DataAnnotations and ASP.NET MVC2 capabilities.

But, they do not work for radio button controls. So I guess the problem is not with xVal, it must be in radio buttons and data annotation combination.