0

I am trying to pass the values from JavaScript string array variable into controller method. The method is called but the value is not being passed. Here is my example Controller

[HttpPost]       
public IActionResult ReinstateEmployeeUpdate(List<string> employeeIds)
{
   return View( );
}
<script type="text/javascript">
function UpdateEmployee() {
        var employeeIds = [];         
        var employeeIds = ["346", "347"];
         
        $.ajax({
            type: "POST",
            contentType: "application/json",
            headers: { 'Content-Type': 'application/json' },
            url: "/Employee/ReinstateEmployeeUpdate",
            dataType: "json",
            data: JSON.stringify(employeeIds),
            success: function (response) {
                console.log(response);
            },

            error: function (response) {
                console.log(response.responseText);
            }
        });
    }
</script>
Alevel
  • 39
  • 5
  • If you pass and receive as string then remove `contentType: "application/json", headers: { 'Content-Type': 'application/json' }, dataType: "json",` You will have to parse the string on the server. – mplungjan Jan 17 '23 at 07:34
  • `JSON.stringify({ employeeIds })` with the `{}` – adiga Jan 17 '23 at 07:49

1 Answers1

1

You could try as below:

[HttpPost]       
    public IActionResult ReinstateEmployeeUpdate([FromBody]List<string> employeeIds)
    {
       return View( );
    }
Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11