How to handle Booleans/CheckBoxes in ASP.NET MVC 2 with DataAnnotations?

Posted by asp_net on Stack Overflow See other posts from Stack Overflow or by asp_net
Published on 2010-02-11T14:47:15Z Indexed on 2010/04/25 23:33 UTC
Read the original article Hit count: 752

I've got a view model like this:

public class SignUpViewModel
{
    [Required(ErrorMessage = "Bitte lesen und akzeptieren Sie die AGB.")]
    [DisplayName("Ich habe die AGB gelesen und akzeptiere diese.")]
    public bool AgreesWithTerms { get; set; }
}

The view markup code:

<%= Html.CheckBoxFor(m => m.AgreesWithTerms) %>
<%= Html.LabelFor(m => m.AgreesWithTerms)%>

The result:

No validation is executed. That's okay so far because bool is a value type and never null. But even if I make AgreesWithTerms nullable it won't work because the compiler shouts

"Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions."

So, what's the correct way to handle this?

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about dataannotations