I work on asp.net MVC application I face issue check box yes or no send false value yes or no when click approve button
I have two check box one represent true and second false as user requested and i don't it to be radio button because bossiness user request it . Yes represent true No represent false
I Face Issue when select Yes or no for first time it send value correctly if Yes then true or No then false but when i select Yes or No for second time it not working correctly and send only false to approve button what i try
1 - user UI for Yes or No
@Html.Label("Did you try to retain the staff?", htmlAttributes: new { @class = "control-label col-md-5" })
Yes
No
Approve2 - when change yes or no check box to get false of retain stuff i use jQuery as below
$('.retaindtuff-checkbox').on('change', function () {
$('.retaindtuff-checkbox').not(this).prop('checked', false);
var selectedValue = $(this).val();
$('#RetainStuff').val(selectedValue);
});
3 - when click button approve it call submit function as below
function submit() {
console.log("check submit");
var ResignationRequester = new Object();
ResignationRequester.RequestNo = document.getElementById("RequestNo").innerHTML.trim();
var RetainStuffElement = document.getElementById("RetainStuff");
if (RetainStuffElement) {
var RetainStuffElementExist = document.querySelector('input[name="RetainStuff"]:checked');
if (RetainStuffElementExist && RetainStuffElementExist.value === "false") {
ResignationRequester.RetainStuff = RetainStuffElementExist.value;
Swal.fire({
icon: 'warning',
title: 'Retain Stuff is Empty',
text: 'retain stuff comment is empty and retain stuff is No '
});
ResignationRequester.RetainStuff = null;
return false;
}
else if (RetainStuffElementExist) {
ResignationRequester.RetainStuff = RetainStuffElementExist.value;
}
else {
ResignationRequester.RetainStuff = null;
Swal.fire({
icon: 'warning',
title: 'Retain Stuff is Empty',
text: 'Retain Stuff Must Not Empty'
});
return false;
}
}
else {
ResignationRequester.RetainStuff = "";
}
if (ResignationRequester != null) {
$.ajax({
}
}4 - action result calling ApprovalIndex on resignation controller
public async Task ApprovalIndex(ResignationRequester REQ)
{
//update approve status
}so why checkbox yes or no working on first time only Image for page i work on it
my image to show screen


Mehmet FatihRank #1,475 · 1.1K reputationPosted Jan 20, 2024, 3:26 AM EST
Have you tried setting value to 0 and 1 instead of true false?
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 20, 2024, 5:05 AM EST
The issue you’re facing is likely due to the fact that you’re using the same
idandnameattributes for both checkboxes. In HTML, theidattribute should be unique within a page. When you’re trying to get the value of the checkbox with$('#RetainStuff').val(selectedValue);, it’s only getting the value of the first checkbox it finds with thatid, which is why it’s always returningfalse.Here’s a potential solution:
idattributes to your checkboxes, likeRetainStuffTrueandRetainStuffFalse.$('.retaindtuff-checkbox').on('change', function () { $('.retaindtuff-checkbox').not(this).prop('checked', false); });submitfunction, check thecheckedproperty of both checkboxes to determine the value.function submit() { var ResignationRequester = new Object(); ResignationRequester.RequestNo = document.getElementById("RequestNo").innerHTML.trim(); var RetainStuffTrue = document.getElementById("RetainStuffTrue"); var RetainStuffFalse = document.getElementById("RetainStuffFalse"); if (RetainStuffTrue.checked) { ResignationRequester.RetainStuff = RetainStuffTrue.value; } else if (RetainStuffFalse.checked) { ResignationRequester.RetainStuff = RetainStuffFalse.value; } else { // Handle case where neither checkbox is checked } // Continue with the rest of your function... }This way, you’re directly checking the
checkedproperty of each checkbox, rather than trying to get the value based on theid12.Thanks