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 = $('');
$.each(response, function (i, item) {
$(subClassesContainer).append(' ' + item.subClassAccountName + '
');
});
$('#subClassesList').append(subClassesContainer);
}
});
Lokesh VarmanPosted Feb 25, 2024, 5:09 AM
It seems like you're trying to pass both the IDs and the text associated with the selected checkboxes to your backend. Your approach looks mostly correct, but there might be an issue with how you're handling the AJAX request or how you're constructing your data to send.
Here are a few suggestions to debug and fix the issue:
1. **Check AJAX Request Payload**: Before sending the AJAX request, ensure that `selectedClassIds` and `selectedClassText` arrays are populated correctly. You can log them to the console to verify.
2. **Verify AJAX Request Data**: Ensure that the data being sent in the AJAX request is properly formatted and includes both `classIds` and `classidsText`.
3. **Check Razor Page Handler**: Ensure that your Razor Page handler (`OnGetSubAccountClassName`) is correctly receiving both `classIds` and `classidsText`. It seems you're correctly receiving `classIds`, but `classidsText` is not being received correctly.
4. **Check Model Binding**: Make sure your backend model is set up to correctly bind the incoming data. The parameter names in the Razor Page handler should match the keys in the AJAX request.
Here's a revised version of your AJAX request code:
Ensure that the `error` callback is implemented to catch any issues with the AJAX request.
Note : AI generated content.