0
// something defined deleteArr and pass values to it
var postData = { deleteArr: deleteArr };
if(deleteArr.length > 0)
    {
        $.ajax({
            url: "@Url.Action("Delete", "ASZ01")",
            type: "POST",
            data: postData,
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                alert("success.");
            },
            error: function (response) {
                alert(deleteArr[0]);
            }
        });
        deleteArr.length = 0;
    }

The above code is javascript. Until $.ajax begin I can confirm that values in array is correct in immediate window,but when it comes to error: I got "undefined". And the following is my function in controller

public void Delete(List<string> deleteArr)
    {
        service.Delete(deleteArr);
    }

The second question is that I set breakpoint on that function but it can't stop. I think maybe my ajax form is wrong?

DeAn
  • 23
  • 6
  • Please refer to this [link](http://stackoverflow.com/questions/309115/how-can-i-post-an-array-of-string-to-asp-net-mvc-controller-without-a-form) – Vidhyadhar Galande Oct 28 '16 at 07:21

2 Answers2

0

Stringify to JSON, add the dataType: 'json' and then pass and also correct your ""

var postData = JSON.stringify({ deleteArr: deleteArr });

if(deleteArr.length > 0)
    {
        $.ajax({
            url: @Url.Action("Delete", "ASZ01"),
            type: "POST",
            data: postData,
            dataType: 'json'
            contentType: "application/json; charset=utf-8",
            success: function (response) {
                alert("success.");
            },
            error: function (response) {
                alert(deleteArr[0]);
            }
        });
        deleteArr.length = 0;
    }
Suren Srapyan
  • 66,568
  • 14
  • 114
  • 112
0

Small change to your postData

var postData = { deleteArr: JSON.stringify(deleteArr) };

Idea is to convert your array data into string format ie:JSON and posting to the server, The default Model binder of MVC framework will handle the part to convert them into List<string> for you

Rajshekar Reddy
  • 18,647
  • 3
  • 40
  • 59