Naughty Ninja Naughty Ninja - 7 months ago 26
Javascript Question

Ajax call passing always true for checkbox data MVC/C#

Ajax call is always passing true for isHalfDay checkbox, every other value that is being passed is correct apart from this one. What might be the reason?

UpdateDays: function () {
$.ajax({
url: $("#numberOfDays").data("url"),
data: {
startDate: $("input[name=StartDate]").val(),
endDate: $("input[name=EndDate]").val(),
employeeId: $("#holiday-editor").data("employee"),
isHalfDay: $("input[name=HalfDay]").val()
},
method: "POST"
}).success(function (response) {
$("#numberOfDays").html(response);
$("#Days").val(response);
});

Answer

It's because the checkbox always has a value. If you want to know if it's checked or not use prop('checked'), like this:

isHalfDay: $("input[name=HalfDay]").prop('checked')

Also note that there is no success() method on the promise returned from the $.ajax call - I think you mean done() instead.