title: In MVC 6, how to code checkbox list in view and pass the checked values to the controller?
body: <p>Sorry but most of my searches take me to old MVC codes.  Any help will be appreciated.</p>
  
  <p>In MVC 6 with tag helpers, how do you code a set of checkboxes:</p>
  
  <ul>
  <li>Use tag helper for label so clicking it will toggle the checked value</li>
  <li>Save (Bind?) the checked value to the IsOptionSelected property </li>
  <li>Pass these checked values back to Controller after clicking Submit</li>
  </ul>
  
  <p>?</p>
  
  <p>I was able to display the checkboxes with labels correctly, but I do not know how to pass the checked values back to the controller via the model.  Right now, IsOptionSelected values are coming back as false.</p>
  
  <p>I was also able to make the html helper for the label work but not for the tag helper.  I may be also coding these all wrong so any tips will help!</p>
  
  <p>Here's what I have so far:</p>
  
  <p><strong>Display:</strong></p>
  
  <p><a href="https://i.stack.imgur.com/xOSHE.png" rel="noreferrer"><img src="https://i.stack.imgur.com/xOSHE.png" alt="Phone Options Checkboxes"></a></p>
  
  <p><strong>Entity:</strong></p>
  
  <pre><code>public class PhoneOption
  {
      public bool IsOptionSelected { get; set; } = false;
      public int OptionId { get; set; }
      public string OptionName { get; set; }
  }
  </code></pre>
  
  <p><strong>Model:</strong></p>
  
  <pre><code>[Display(Name = "Phone Options")]
  public IEnumerable&lt;PhoneOption&gt; PhoneOptions { get; set; }
  
  . . . .
  PhoneOptions = repository.GetPhoneOptions();
  </code></pre>
  
  <p><strong>Repository:</strong></p>
  
  <pre><code>public IEnumerable&lt;PhoneOption&gt; GetPhoneOptions()
  {
      IEnumerable&lt;PhoneOption&gt; options = new[]
      {
          new PhoneOption { OptionId = 1, OptionName = "Phone Case",       IsOptionSelected = false },  
          new PhoneOption { OptionId = 2, OptionName = "Screen Protector", IsOptionSelected = false },
          new PhoneOption { OptionId = 3, OptionName = "Car Charger",      IsOptionSelected = false },
          new PhoneOption { OptionId = 4, OptionName = "Extra Cable",      IsOptionSelected = false }
      };
      return options;
   }
  </code></pre>
  
  <p><strong>View:</strong></p>
  
  <pre><code>&lt;div class="form-group"&gt;
      &lt;label class="control-label"&gt;Phone Options&lt;/label&gt;
      &lt;div&gt;
          @foreach (var option in Model.PhoneOptions)
          {
              &lt;div&gt;
                  @{ string cbId = "PhoneOption_" + @option.OptionId; }
                  &lt;input asp-for=@option.IsOptionSelected type="checkbox" value=@option.IsOptionSelected id=@cbId name=@cbId /&gt;
                  @Html.Label(@cbId.ToString(), @option.OptionName)
                  @*This is causing invalid operation exception*@
                  @*&lt;label asp-for=@cbId.ToString()&gt;@option.OptionName&lt;/label&gt;*@ 
                  &lt;span asp-validation-for=@cbId class="text-danger" role="alert"&gt;&lt;/span&gt;
              &lt;/div&gt;
          }
      &lt;/div&gt;    
  &lt;/div&gt;
  </code></pre>
  
  <p>Thanks in advance!</p>