title: Why do my checkboxes not pass their value to my controller in ASP.NET Core?
body: <p>I feel this may be an easy fix but I cannot seem to get around it.  I have an ASP.NET Core web application and I'm using an ajax form to submit data to my controller for processing.  The problem I have is that my checkboxes always pass in as "false" even though they may be checked.</p>
  
  <p>I searched for some answers but nothing I've tried has worked.  Things like using the <code>checked</code> property, ensuring the name is correct.</p>
  
  <p>Can anyone shed some light on why the values are ignored when the form is submitted to the controller and how I can fix it?</p>
  
  <p><strong>Form</strong></p>
  
  <pre><code>&lt;form asp-action="CreateCustomView" asp-controller="Data"
          data-ajax="true"
          data-ajax-method="POST"&gt;
          &lt;div class="row"&gt;
              &lt;div class="col"&gt;
                  &lt;div class="custom-control custom-checkbox"&gt;                            
                      &lt;input type="checkbox" class="custom-control-input" id="ColName" name="ColName" checked&gt;
                      &lt;label class="custom-control-label" for="ColName"&gt;Name&lt;/label&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
  
              &lt;button type="reset"&gt;Reset&lt;/button&gt;
              &lt;button type="submit"&gt;Save changes&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>namespace MyProject.Data
  {
      public class GridView : BaseEntity
      {
          public string Name { get; set; }               
          public bool ColName { get; set; }
      }
  }
  </code></pre>
  
  <p><strong>DataController</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using MyProject.Data;
  using Microsoft.AspNetCore.Mvc;
  
  namespace MyProject.UI.Controllers
  {
      public class DataController : Controller
      {
          public IActionResult Index()
          {
              return View();
          }
          [HttpPost]
          [ValidateAntiForgeryToken]
          public IActionResult CreateCustomView(GridView data)
          {
              //This method is incomplete, there is a break at the ActionResult to see what values are passed into `data`.  In production, the values would be handled and the changes would be saved.
              return View();
          }
      }
  }
  </code></pre>
  
  <p>I have a breakpoint on the method <code>CreateCustomView</code> to see what values are passed, no matter what checkboxes I check, they are always being passed as <code>false</code>.</p>
  
  <p>Can anyone help with this rather strange problem?</p>
  
  <p><strong>Update</strong></p>
  
  <p>As per the answer supplied by @Reyan-Chougle, if you're using CORE you need to supply the input as follows:</p>
  
  <pre><code>&lt;input asp-for="@Model.ColType" type="checkbox" /&gt;
  </code></pre>
  
  <p>When the page is rendered, the control is automatically given a hidden field.  Here is what the HTML of the above control renders as:</p>
  
  <pre><code>&lt;input type="checkbox" class="custom-control-input" id="ColType" value="true" data-val="true" data-val-required="The ColType field is required." name="ColType"&gt;
  &lt;input type="hidden" value="false" id="ColType" name="ColType"&gt;
  </code></pre>
  
  <p>As you can see it creates a hidden checkbox with a value of false.  This means that, as a boolean type it always has a value of true or false on submission.</p>