Archive for July, 2016

MVC Checkbox always true

In my MVC view, I had checkbox, like so:

@Html.CheckBoxFor(m => m.IsComplete)

A button click event called a jQuery method to pull the value of the checkbox to send it to a Web API call:

$("#IsComplete").val()

The problem was that the checkbox value in jQuery was always true, whether or not it was checked on the view. To correct this, I had to abandon the “val()” method and instead used the “:checked” operator:

   var isChecked = $("input[name='IsComplete']:checked").length;
   var isComplete = false;

   if (isChecked == 1) {
       isComplete = true;
   }

Leave a comment