title: "Select All" Checkboxes using Blazor
body: <p>How can I implement a &quot;Select All&quot; checkbox using Blazor for a collection of checkboxes? There are plenty of examples using JavaScript/jQuery, but I was trying to do it with C# instead.</p>
  <p>Here's what I'm working with right now. This code works adding the months selected to the list, but when using the &quot;select all&quot; checkbox, the other boxes are not showing checked.</p>
  <p>Note: <code>@Months[0].MonthName = Select All</code></p>
  <pre><code>&lt;ul class=&quot;checkbox-grid&quot;&gt;
    &lt;li&gt;
      &lt;input type=&quot;checkbox&quot;
        @onchange=&quot;EventArgs =&gt; { CheckAllMonths(Months[0], EventArgs.Value); }&quot; /&gt;
      &lt;label for=&quot;@Months[0].MonthId&quot; id=&quot;checkboxLabel&quot;&gt;@Months[0].MonthName&lt;/label&gt;
    &lt;/li&gt;
  &lt;/ul&gt;
  @{
    for (int i = 1; i &lt; Months.Count(); i++)
    {
      int j = i;
      &lt;ul class=&quot;checkbox-grid&quot;&gt;
        &lt;li&gt;
          &lt;input type=&quot;checkbox&quot; name=&quot;@Months[j].IsMonthChecked&quot;
            @onchange=&quot;EventArgs =&gt; { CheckManualMonth(Months[j], EventArgs.Value); }&quot; /&gt;
          &lt;label for=&quot;@Months[j].MonthId&quot; id=&quot;checkboxLabel&quot;&gt;@Months[j].MonthName&lt;/label&gt;
        &lt;/li&gt;
      &lt;/ul&gt;
    }
  }
  
  @code {
    List&lt;Month&gt; MonthList = new List&lt;Month&gt;();
  
    void CheckAllMonths(Month month, object checkValue)
    {
      if((bool)checkValue)
      {
        foreach(var item in Months)
        {
          CheckManualMonth(item, checkValue);
        }
      }
      else
      {
        foreach(var item in Months)
        {
          CheckManualMonth(item, checkValue);
        }
      }
    }
  
    void CheckManualMonth(Month month, object checkedValue)
    {
      if((bool)checkedValue)
      {
        if(!MonthList.Contains(month))
        {
          MonthList.Add(month);
          month.IsMonthChecked = true;
        }
      }
      else
      {
        if(MonthList.Contains(month))
        {
          MonthList.Remove(month);
          month.IsMonthChecked = false;
        }
      }
    }
  }
  </code></pre>
  <h3>Initialize Months</h3>
  <pre><code>  private void GetMonthChecklist()
    {
      List&lt;Month&gt; months = new List&lt;Month&gt;()
      {
        new Month{MonthId = 0, MonthName = &quot;All Months&quot;, IsMonthChecked = false},
        new Month{MonthId = 1, MonthName = &quot;Jan&quot;, IsMonthChecked = false},
        new Month{MonthId = 2, MonthName = &quot;Feb&quot;, IsMonthChecked = false},
        new Month{MonthId = 3, MonthName = &quot;Mar&quot;, IsMonthChecked = false},
        new Month{MonthId = 4, MonthName = &quot;Apr&quot;, IsMonthChecked = false},
        new Month{MonthId = 5, MonthName = &quot;May&quot;, IsMonthChecked = false},
        new Month{MonthId = 6, MonthName = &quot;Jun&quot;, IsMonthChecked = false},
        new Month{MonthId = 7, MonthName = &quot;Jul&quot;, IsMonthChecked = false},
        new Month{MonthId = 8, MonthName = &quot;Aug&quot;, IsMonthChecked = false},
        new Month{MonthId = 9, MonthName = &quot;Sep&quot;, IsMonthChecked = false},
        new Month{MonthId = 10, MonthName = &quot;Oct&quot;, IsMonthChecked = false},
        new Month{MonthId = 11, MonthName = &quot;Nov&quot;, IsMonthChecked = false},
        new Month{MonthId = 12, MonthName = &quot;Dec&quot;, IsMonthChecked = false}
      };
  
      Months = months.ToList();
    }
  </code></pre>