title: Cannot get CheckBox List to work in ASP.NET Core
body: <p>I am building a web application in ASP.NET Core 1.1 (MVC) using VS.2017 Community. In it I have a view in which I have to display a list of checkboxes and then when the form is submitted the model contains among other things the list of checkboxes that were checked. However, when the POST is done, the other properties are passsed well but the List&lt;> property has the correct length (one for each checkbox, marked or not) but the IsChecked property of all of them is <strong>false</strong> even when the checkbox was checked. </p>
  
  <p>My View Model that is passed to the view looks like this:</p>
  
  <pre><code>public class UserPermissionsViewModel {
      public string Id { get; set; }
      public string UserName { get; set; }
      public List&lt;CheckboxListItem&gt; PermissionList { get; set; }
  
      public UserPermissionsViewModel() {
          PermissionList = new List&lt;CheckboxListItem&gt;();
      }
  }
  
  public class CheckboxListItem {
      public int ID { get; set; }
      public string Display { get; set; }
      public bool IsChecked { get; set; }
  }
  </code></pre>
  
  <p>And the relevant part of the view where I render the list of checkboxes looks like this:</p>
  
  <pre><code>@model Models.UserPermissionsViewModel
  
  @{   // code block in the middle of the view, not at the top!
       foreach (CheckboxListItem cbitem in Model.PermissionList) {
           &lt;div class="checkbox"&gt;
              &lt;div class="form-inline col-md-offset-2"&gt;
                 &lt;input type="hidden" asp-for="@Model.PermissionList[i].ID" /&gt;
                 &lt;input type="hidden" asp-for="@Model.PermissionList[i].Display" /&gt;
                 &lt;input type="checkbox" asp-for="@Model.PermissionList[i].IsChecked" value="@Model.PermissionList[i].IsChecked" /&gt;
                 &lt;label asp-for="@Model.PermissionList[i].ID"&gt;@Model.PermissionList[i].Display&lt;/label&gt;
              &lt;/div&gt;
           &lt;/div&gt;
       }
  }
  </code></pre>
  
  <p>And the Post controller action (simplified):</p>
  
  <pre><code>[HttpPost]
  [ValidateAntiForgeryToken]
  public IActionResult PermissionAssignToUser(UserPermissionsViewModel model)
  {
       if (ModelState.IsValid) {
           // it gets here but the model.PermissionList pty has ZERO length.
       }
  }
  </code></pre>
  
  <p>So how do I actually get the list of values right, thus indicating that for example checkbox with ID = 1 is true and checkbox with ID = 3 is true? So far I get all of them but with value <strong>false</strong> instead of the actual checkbox state.</p>
  
  <p><strong>UPDATE #3</strong>
  After much trial I actually got it to work and found out WHY the states of the checkboxes were not being passed to the Post action method.</p>
  
  <p>If you can see above my <em>input tag helper</em> for the <em>checkbox type</em> not only has an <strong>asp-for</strong> property but also a value property. I thought that with it I would have it display the current value (when the view was rendered). But as it turns out, if you specify a <strong>value</strong> (or <strong>checked</strong>) property in addition to the <strong>asp=for</strong> property then the POST action method will always receive false regardless of the actual state of the checkbox. </p>
  
  <p>If on the other hand for the <em>checkbox type</em> you only specify the <strong>asp-for</strong> property, then the checkbox state is properly communicated via the model to the POST action method.</p>
  
  <p>So replacing the checkbox input above for this one below actually solves the problem:</p>
  
  <pre><code>&lt;input type="checkbox" asp-for="@Model.PermissionList[i].IsChecked" /&gt;
  </code></pre>