0

I've read about how to get validation of my MVC Model working. It's pretty cool.

But I need to validate a strictly UI situation, that doesn't relate to the model. Specifically, I need to check that the user clicked one of several checkboxes before clicking a link. Let me stress, again, that the checkboxes don't represent model data: they're strictly for View and Controller purposes.

In the WebForms world, I'd just stick in a RequiredFieldValidator. What's the equivalent for a field that DOESN'T represent a field on a model?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Ann L.
  • 13,760
  • 5
  • 35
  • 66

3 Answers3

1

You will need to do some custom model binding to check the request object and act accordingly (adding modelstate errors, etc...) Here is a post related to custom model binding that should be beneficial.

ASP.Net MVC Custom Model Binding explanation

Once in the custom model binder you can add code in the CreateModel method similar to this:

Dim request As HttpRequestBase = controllerContext.HttpContext.Request
If Not request.Form.AllKeys.Contains("YourCheckBoxName") Then
     bindingContext.ModelState.AddModelError("AnError", "You must check the box first")
End If
Community
  • 1
  • 1
ooPeanutButter
  • 433
  • 2
  • 9
  • Thank you, and that's very interesting. I hadn't heard of doing that before. – Ann L. Oct 07 '11 at 14:42
  • @AnnL. Additionally, if you're looking for just a client side solution, you could create a ViewModel that essentially extends your model, make the view take your new ViewModel instead of the Model and add the "checkbox" fields as properties to the ViewModel. You could then use the default model binding included with MVC. See this link for more info: http://geekswithblogs.net/michelotti/archive/2009/10/25/asp.net-mvc-view-model-patterns.aspx – ooPeanutButter Oct 07 '11 at 15:51
  • Thank you. This gave me some great ideas! – Ann L. Oct 16 '11 at 15:23
0

You can use jquery validation to inject validation rules into the validationset.

Roger Far
  • 2,178
  • 3
  • 36
  • 67
  • Interesting, and thank you. This would be a strictly jQuery solution, then -- nothing to do with the MVC framework? – Ann L. Oct 07 '11 at 14:40
-1

Quick and dirty solution using a css class name to check if one of the boxes have been checked.

<input type="checkbox" value="1" name="something" class="boxGroupName" /> Check1
<input type="checkbox" value="2" name="another" class="boxGroupName" /> Check1
<input type="checkbox" value="3" name="third" class="boxGroupName" /> Check1

<input type="submit" id="buttonId" />    

<script type="text/javascript">
$(function() {
    $('#buttonId').click(function() {
        if ($('.boxGroupName:checked').length == 0) {
            alert('Check some boxes, ehh?');
            return false;
        }
        return true;
    });
});
</script>
jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • Thank you. What I was hoping for, though, was some way to tap into MVC client-side validation, and have my validation message appear in a ValidationSummary, etc. But I probably need a better understanding of just how MVC 3 client-side validation works, first. Thanks again! – Ann L. Oct 07 '11 at 14:44