I work on asp.net core razor page . i face issue i can't pass selected ids and selected text from checkboxes from action
so how to pass classIds from action OnGetSubAccountClassName to action post OnPostGenerateCount using hidden field
public JsonResult OnGetAccountClassName()
{
var assetsAccountName = _IAssetsService.GetJdeAssetAccountClassName();
AssetCountGeneration.JDEAssetAccountClassNameDetails = assetsAccountName;
return new JsonResult(assetsAccountName);
}
public JsonResult OnGetSubAccountClassName(string[] classIds,string [] classIdsValues)
{
var assetsSubAccountName = _IAssetsService.GetJdeAssetSubAccountClassName(classIds);
AssetCountGeneration.JDEAssetSubAccountClassNameDetails = assetsSubAccountName;
return new JsonResult(assetsSubAccountName);
}
public JsonResult OnPostGenerateCount(AssetCountGeneration AssetCount)
{
}
i fill list when click button as below
$('#btnDisplay').click(function (event) {
event.preventDefault();
dropdown.empty();
$.ajax({
url: '?handler=AccountClassName',
type: "GET",
dataType: "json",
success: function (response) {
$('#classesContainer').show();
$('#classesList').html(''); // Clear existing classes
$('#classesList').append(' All
');
$.each(response, function (i, classes) {
$('#classesList').append(' ' + classes.classAccountName + '
');
});
}
});
});
and when i make change selection on menu class
$(document).on('change', '.classCheckbox', function () {
var selectedClassIds = [];
var selectedClassIdsValues = []; // Array to store selected values
$('.classCheckbox:checked').each(function () {
selectedClassIds.push($(this).val());
selectedClassIdsValues.push($(this).next('span').text());
});
console.log("selected vvvvvv" + selectedClassIdsValues)
if (selectedClassIds.length > 0) {
$.ajax({
url: '?handler=CheckedAccountClassName',
type: 'GET',
traditional: true,
data: { classIds: selectedClassIds, classIdsValues: selectedClassIdsValues },
success: function (response) {
$('#subClassesList').empty();
$('#subClassesContainer').show();
// console.log("value selected is" + $('#dataList').val())
var subClassesContainer = $('');
$.each(response, function (i, item) {
$(subClassesContainer).append(' ' + item.subClassAccountName + '
');
});
$('#subClassesList').append(subClassesContainer);
}
});
and for html code as below
Classes
Subclasses
and classes used to checkbox list on csharp as below
public class AssetCountGeneration
{
public List JDEAssetAccountClassNameDetails { get; set; }
}
public class JDEAssetAccountClassName
{
public string ClassAccountId { get; set; }
public string ClassAccountName { get; set; }
public bool ClassCheck { get; set; }
}
Prasad RaveendranPosted Feb 27, 2024, 1:40 AM
Update the
OnPostGenerateCountaction to include the parameters:In your Razor Page HTML, make sure you include the hidden fields within the form element:
Modify your JavaScript code to set the values of hidden fields before submitting the form:
Now, when the form is submitted, the values of
selectedClassIdsandselectedClassIdsValueswill be passed to theOnPostGenerateCountaction along with the other form elements. Ensure that your form has the correct method (post) and action attribute. Adjust the action method logic according to your requirements.ahmed salahPosted Feb 26, 2024, 5:33 AM
thanks for support
how to pass these two values
string[] classIds = HttpContext.Request.Query["selectedClassIds"].ToString().Split(','); string[] classIdsValues = HttpContext.Request.Query["selectedClassIdsValues"].ToString().Split(',');
to onpostgenerate action
public JsonResult OnPostGenerateCount(AssetCountGeneration AssetCount) { }
Prasad RaveendranPosted Feb 26, 2024, 1:56 AM
To pass the selected IDs and values from checkboxes in the
OnGetSubAccountClassNametoOnPostGenerateCountusing a hidden field, you can make use of a hidden field in your Razor Page. Here's an example of how you can modify your code:Add a hidden field in your Razor Page HTML:
Update your JavaScript code to set the values of hidden fields when checkboxes are selected:
Modify your
OnGetSubAccountClassNameto use the values from hidden fields:Now, when you click the button, the selected IDs and values will be stored in the hidden fields, and these values will be retrieved in the
OnGetSubAccountClassNameaction. Ensure that you handle potential null or empty values appropriately based on your application logic.