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>
2
  • 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. Commented Jan 17, 2023 at 7:34
  • JSON.stringify({ employeeIds }) with the {} Commented Jan 17, 2023 at 7:49

1 Answer 1

1

You could try as below:

[HttpPost]       
    public IActionResult ReinstateEmployeeUpdate([FromBody]List<string> employeeIds)
    {
       return View( );
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.