i am using a check box to validate the input given by user. the view is as follows
JavaScript
// this is the scrpt for checkbox
$(document).on("click", "[type='checkbox']", function (e) {
if (this.checked) {
$(this).attr("value", "1");
} else {
$(this).attr("value", "0");
}
});
@if(Model.Is_Prefix_Checked == 1)
{
}
else
{
}
PrefixWhen the check box is unchecked it passes null instead of 0
in my model i as follows
C#
public int? isprefixchecked {get; set;}Kindly help

Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 7, 2023, 12:10 AM EST
Hello Kaushik,
Your model property
isprefixcheckedis of typeint?.which is expecting either an integer value or null. So you need to cast the value to an integer before setting it to the model property$(document).on("click", "[type='checkbox']", function (e) { var value = this.checked ? 1 : 0; $(this).attr("value", value); });Thanks
Naimish Makwana
Sachin SinghRank #66 · 26.2K reputationPosted Feb 7, 2023, 12:33 AM EST
Test this
$('input[type="checkbox"]').change(function(){ this.value = (Number(this.checked)); });Rijwan AnsariRank #82 · 24.2K reputationPosted Feb 7, 2023, 7:14 AM EST
Hi
Select your checkbox with Id reference.
Example:
const checkbox = document.getElementById('Is_Prefix_Checked') checkbox.addEventListener('change', (event) => { var value = this.checked ? 1 : 0; $(this).attr("value", value); })