Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

JavaScript
@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); // THIS IS CORRECT
            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:

C#
[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.
Posted

1 solution

Quote:
JavaScript
var arrayOfFeatureIds = []; // Declare a local variable
...
arrayOfFeatureIds.push(...); // Push values into the local variable
...
console.log(arrayOfFeatureIds); // Log the local variable

passData = JSON.stringify(this.arrayOfFeatureIds); // Use an undefined property from the global namespace!
Remove this. from the JSON.stringify line. You're referring to a completely different, and most likely undefined, object.

Quote:
C#
[HttpPost]
public JsonResult UpdateMap(string[] ids)
{
    return Json("Success");
}
You may need to add [FromBody] to the parameter; I suspect it's trying to bind the values from the query-string instead.
C#
[HttpPost]
public JsonResult UpdateMap([FromBody] string[] ids)
 
Share this answer
 
v2
Comments
RobertHitchins 9-Jul-24 12:06pm    
Hi Richard...Thanks!! It was the [FromBody] missing from the Controller method. I had already tried both with and without the "this" object. Appreciate the assistance!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900