kaushik guru

kaushik guru

  • NA
  • 252
  • 30.5k

when checkbox is unchecked it passes null value instead of 0

Feb 7 2023 5:02 AM

i am using a check box to validate the input given by user.  the view is as follows

<script>
    // 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");
        }
    });
</script>

<div class="row mb-2">
    @if(Model.Is_Prefix_Checked == 1)
    {
        <div>
        <div class="col-md-1" style='align-content:center;'>
            <input type="checkbox"  class="largerCheckbox" id="Is_Prefix_Checked" name="IsPrefixChecked" value="1" checked="checked" >
        </div>
    </div>
    }
    else
    {
        <div>
        <div class="col-md-1" style='align-content:center;'>
                <input type="checkbox" class="largerCheckbox" id="Is_Prefix_Checked" value="0" name="IsPrefixChecked">
        </div>
    </div>
    }
    <label class="col-sm-2 col-form-label">Prefix</label>
    <div class="col-sm-3">
        <select asp-for="EmployeePrefix" class="form-control" asp-items="@ViewBag.prefix"  >
        </select>
        <span asp-validation-for="EmployeePrefix" class="text-danger" ></span>
    </div>
</div>

When the check box is unchecked it passes null instead of 0 

in my model i as follows

public int? isprefixchecked {get; set;}

Kindly help


Answers (3)