title: Making checkboxlist custom validation in asp.net mvc 5
body: <p>I am developing an ASP.Net MVC 5 Web application and I am having some difficulties with making custom validation on Checkbox list. Validation I need that at least one checkbox must be checked</p>
  
  <p>My ViewModel</p>
  
  <pre><code>public class EditUtilisateurViewModel
  {
  public long Id { get; set; }
  
  [Required(AllowEmptyStrings = false, ErrorMessage = " Required ")]
  [Display(Name = "Login")]
  public string UserName { get; set; }
  
  [Required(AllowEmptyStrings = false, ErrorMessage = "Required")]
  [Display(Name = "Email")]
  [EmailAddress (ErrorMessage = "Invalid Email")]
  public string Email { get; set; }
  
  [CheckOneSelected(ErrorMessage = "Please select role")]
  public IEnumerable&lt;System.Web.Mvc.SelectListItem&gt; RolesList { get; set; }
  
  }
  </code></pre>
  
  <p>My Controller</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public async Task&lt;ActionResult&gt; Edit([Bind(Include = "Id,UserName,Email")] EditUtilisateurViewModel editUser, params string[] selectedRole)
  {
  if (ModelState.IsValid)
  {
      // ToDo ...
  
      return RedirectToAction("Index");
  }
  }
  </code></pre>
  
  <p>My View</p>
  
  <pre><code>@model MyProject.Areas.Admin.Models.EditUtilisateurViewModel
  
  @using (Html.BeginForm())
  {
      @Html.AntiForgeryToken()
      @Html.ValidationSummary(true)
      @Html.HiddenFor(m =&gt; m.Id)
      &lt;table
          &lt;tbody&gt;
              &lt; &gt;
                  &lt;th&gt;
                      @Html.LabelFor(m =&gt; m.UserName)
                  &lt;/th&gt;
                  &lt;td&gt;
                      @Html.TextBoxFor(m =&gt; m.UserName)
                      @Html.ValidationMessageFor(m =&gt; m.UserName)
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;th&gt;
                      @Html.LabelFor(m =&gt; m.Email)
                  &lt;/th&gt;
                  &lt;td&gt;
                      @Html.TextBoxFor(m =&gt; m.Email, new { @class = "form-control", @disabled = "disabled" })
                      @*@Html.ValidationMessageFor(m =&gt; m.Email)*@
                  &lt;/td&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                  &lt;th&gt;@Html.Label("Roles")&lt;/th&gt;
                  &lt;td&gt;
                      &lt;span&gt;
                          @foreach (var item in Model.RolesList)
                          {
                              @*&lt;input type="checkbox" id="@item.Value" name="SelectedRole" value="@item.Value" checked="@item.Selected" class="checkbox-inline" /&gt;*@
                              @Html.CheckBoxFor(m =&gt; item.Selected, new { id = item.Value, @Value = item.Value, @Name = "selectedRole[]", @class = "checkbox-inline", data_val = "true", data_val_verifListe = " Select field " })
                              @Html.Label(item.Value, new { @class = "checkbox-inline-label" })
                          }
                      &lt;/span&gt;
                      @Html.ValidationMessageFor(m =&gt; m.RolesList)
                  &lt;/td&gt;
              &lt;/tr&gt;
          &lt;/tbody&gt;
      &lt;/table&gt;
      &lt;p&gt;
          &lt;input type="submit" value="Update" /&gt;
      &lt;/p&gt;
  }
  </code></pre>
  
  <p>I tried Validation like below but and have essues</p>
  
  <pre><code>namespace ...
  {
      public class CheckOneSelectedAttribute : ValidationAttribute, IClientValidatable
      {
          public string[] SelectedRole { get; private set; }
  
          public CheckOneSelectedAttribute(string SelectedValue) : base("Select field")
          {
              if (SelectedValue != null &amp;&amp; SelectedValue.Length &gt; 0)
                  SelectedRole = SelectedValue;
          }
  
          protected override ValidationResult IsValid(object value, ValidationContext validationContext)
          {
              if (value != null)
              {
                  if (selectedRole != null)
                  {
                      if (selectedRole.Length == 0)
                      {
                          return new ValidationResult("Select field ");
                      }
                  }
              }
              else
              {
                  return new ValidationResult("Null parameter");
              }
              return ValidationResult.Success;
          }
  
          public IEnumerable&lt;ModelClientValidationRule&gt; GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
          {
              yield return new ModelClientValidationRule()
              {
                  ErrorMessage = ErrorMessageString,
                  ValidationType = "CheckOneSelected"
              };
          }
      }
  }
  </code></pre>
  
  <p>Can some one please help me?
  Thanks.</p>