I have an ASP.NET Core 8 view with a script that collects Id's from a table of checkboxes. The script collects them correctly into an arrayOfFeatureIds. I have checked the values in the console and they are correct. The JavaScript I am working with is:
@section scripts {
<script>
document.getElementById('updateMap').addEventListener('click', () => {
var arrayOfFeatureIds = [];
var selected;
$("#features tbody tr").each(function () {
selected =
$(this).find("input[type='checkbox']:checked").val();
if (selected) {
arrayOfFeatureIds.push($(this)
.find("input[type='hidden']").val());
}
});
console.log(arrayOfFeatureIds);
passData = JSON.stringify(this.arrayOfFeatureIds);
$.ajax({
url: "/Maps/UpdateMap",
type: "POST",
data: passData,
contentType: 'application/json',
dataType: "json",
success: function () {
alert("OK");
},
error: function (errMsg) {
alert(errMsg);
}
});
});
</script>
}
The script successfully POSTs back to the Controller. The Controller code is:
[HttpPost]
public JsonResult UpdateMap(string[] ids)
{
return Json("Success");
}
However, the string[] is always empty. I have tried multiple ways to send it in the JavaScript, but no luck. Can anybody let me know where I'm going wrong?
What I have tried:
I've tried multiple ways to stringify the array:
1. data: JSON.stringify(arrayOfFeatureIds)
2. passData = JSON.stringify(arrayOfFeatureIds)
3. passData = JSON.stringify(this.arrayOfFeatureIds)
I've also gone through several articles and tried the solutions on this site, but no luck.