I work on asp.net core razor page . I pass selected id as array success
but my issue selected text value for check box not passed or displayed
Issue happen on this line selectedClassText.push($(this).next('label').text());
when debug action result string[] classidsText
it display null .
Exactly i need to pass selected text beside checkbox as array.
razor page .cs that get class id text
public JsonResult OnGetSubAccountClassName(string[] classIds,string[] classidsText)
{
var assetsSubAccountName = _IAssetsService.GetJdeAssetSubAccountClassName(classIds);
AssetCountGeneration.JDEAssetSubAccountClassNameDetails = assetsSubAccountName;
return new JsonResult(assetsSubAccountName);
}
from jQuery ajax on page cshtml i send selected displayed text by selectedClassText
$(document).on('change', '.classCheckbox', function () {
var selectedClassIds = [];
var selectedClassText = [];
$('.classCheckbox:checked').each(function () {
selectedClassIds.push($(this).val());
selectedClassText.push($(this).next('label').text());
});
console.log("selected items" + selectedClassIds)
if (selectedClassIds.length > 0) {
$.ajax({
url: '?handler=SubAccountClassName',
type: 'GET',
traditional: true,
data: { classIds: selectedClassIds,classidsText:selectedClassText },
success: function (response) {
$('#subClassesList').empty();
$('#subClassesContainer').show();
var subClassesContainer = $('<div data-class-id="' + selectedClassIds + '"></div>');
$.each(response, function (i, item) {
$(subClassesContainer).append('<input type="checkbox" class="subclassCheckbox" name="selectedSubClasses" value="' + item.subClassAccountId + '" /> ' + item.subClassAccountName + '<br />');
});
$('#subClassesList').append(subClassesContainer);
}
});
Updated post
I try code below but not worked so how to solve this issue
console.log() and it give me null
when create checkbox list before make change event
i create it as below
$('#btnDisplay').click(function (event) {
event.preventDefault();
var dropdown = $('#accountclassname-select');
dropdown.empty();
$.ajax({
url: '?handler=AccountClassName',
type: "GET",
dataType: "json",
success: function (response) {
$('#classesContainer').show();
$('#classesList').html(''); // Clear existing classes
$('#classesList').append('<input type="checkbox" class="classCheckbox" value="0" /> All <br />');
$.each(response, function (i, classes) {
$('#classesList').append('<input type="checkbox" class="classCheckbox" value="' + classes.classAccountId + '" /> ' + classes.classAccountName + '<br />');
});
}
});
});
Important notes
I don’t have label to append text on it
i create list as below
$(subClassesContainer).append('<input type="checkbox" class="subclassCheckbox" name="selectedSubClasses" value="' + item.subClassAccountId + '" /> ' + item.subClassAccountName + '<br />');
});
2
Answers
Try Like This
I’m assuming
item.subClassAccountName
is the string you want to push intoselectedClassText
if its checkbox is selected.Since it is not inside an element, this is needlessly difficult. Put it inside a
<label>
and your original code should work:Because this code looks for the element immediately following the checkbox, but only if it is a label.
It will stop working if you put anything in between the checkbox and the label.