title: ASP.Net MVC: How to force user to select any checkbox by data annotation
body: <p>i have showing many <code>checkboxes</code> for hobbies property in view model but i like to attach <code>ValidationMessageFor</code> as a result user should be force to select any one checkbox out of many.</p>
  
  <h2>so here is model &amp; viewmodel</h2>
  
  <pre><code>public class Hobby
  {
      public string Name { get; set; }
      public bool IsSelected { get; set; }
  }
  
  public class SampleViewModel
  {
  
      [Required(ErrorMessage = "Select any Hobbies")]
      public List&lt;Hobby&gt; Hobbies { get; set; }
  }
  </code></pre>
  
  <h2>action method code</h2>
  
  <pre><code>public ActionResult Index()
  {
      var SampleVM = new SampleViewModel();
      SampleVM.Products = new List&lt;Product&gt;
      {
          new Product{ ID=1, Name="IPhone" },
          new Product{ ID=2, Name="MacBook Pro" },
          new Product{ ID=3, Name="iPod" }           
      };
  
      SampleVM.Hobbies = new List&lt;Hobby&gt;()
      {
          new Hobby(){ Name = "Reading", IsSelected= false },
          new Hobby(){ Name = "Sports" ,IsSelected= false},
          new Hobby(){ Name = "Movies" ,IsSelected= false}
      };
      return View(SampleVM);
  }
  
  [HttpPost]
  public ActionResult Index(SampleViewModel vm)
  {
      ViewBag.IsPostBack = false;
  
      if (ModelState.IsValid)
      {
          Product p = vm.Products.Where(x =&gt; x.ID == vm.SelectedProductId).FirstOrDefault();
          ViewBag.IsPostBack = true;
          ViewBag.ProductID = p.ID.ToString();
          ViewBag.ProductName = p.Name.ToString();
          return View(vm);
      }
      else
      {
          var SampleVM = new SampleViewModel();
          SampleVM.Products = new List&lt;Product&gt;
          {
              new Product{ ID=1, Name="IPhone" },
              new Product{ ID=2, Name="MacBook Pro" },
              new Product{ ID=3, Name="iPod" }           
          };
          SampleVM.Hobbies = new List&lt;Hobby&gt;()
          {
              new Hobby(){ Name = "Reading", IsSelected= false },
              new Hobby(){ Name = "Sports" ,IsSelected= true},
              new Hobby(){ Name = "Movies" ,IsSelected= true}
          };
  
          return View(SampleVM);
      }
  </code></pre>
  
  <h2>view code</h2>
  
  <pre><code>&lt;div class="form-group"&gt;
      &lt;div class="col-md-offset-2 col-md-10"&gt;
          &lt;b&gt;Hobbies&lt;/b&gt;&lt;br /&gt;
          @for (int x = 0; x &lt; Model.Hobbies.Count(); x++)
          {
              @Html.CheckBoxFor(p =&gt; p.Hobbies[x].IsSelected) @: &amp;nbsp; 
              @Html.LabelFor(p =&gt; p.Hobbies[x].IsSelected, Model.Hobbies[x].Name) @: &amp;nbsp;
          }
      &lt;/div&gt;
      @Html.ValidationMessageFor(model =&gt; model.Hobbies, "", new { @class = "text-danger" })
  &lt;/div&gt;
  </code></pre>
  
  <p>so tell me what to change in code as a result validation message will fire if user select no check boxes for hobbies.</p>
  
  <p>thanks</p>