I have a viewmodel like this :
public UserViewModel
{
public bool IsBlue { get; set; }
public bool IsRed { get; set; }
}
And an associated razor view like this :
<td>
<label for="IsBlue">
<span>is blue ?</span>
</label>
</td>
<td>
<span>@Html.CheckBoxFor(d => d.IsBlue)</span>
</td>
<td>
<label for="IsRed">
<span>is red ?</span>
</label>
</td>
<td>
<span>@Html.CheckBoxFor(d => d.IsRed)</span>
</td>
I have a problem on the server validation side:
The user can check the first, the second, or both textboxes. My question was how can I use the System.ComponentModel.DataAnnotations to force at least one checkbox to be checked. I was wondering if there was like a required attribute to use on the 2 properties.
Thanks in advance for your help.
false, add aModelStateerror -ModelState.AddModelError("", "You must select at least one")and return the view. The error will be displayed in the@Html.ValidationSummary()