如何从jquery ajax向asp.net MVC方法传递2d数组?

时间:2022-11-20 21:34:07

My ajax method:

我的ajax方法:

$.ajax({
    url: actionURL,
    type: 'POST',
    dataType: 'json',
    data: { values: data },
    success: function (data) {
    }
});

My controller method:

我的控制器方法:

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult SaveParameter(string [][] values)
        {
            ...
        }

I have tried a bunch of things, but nothing is working. How can I compose 'data' in javascript??

我试过一堆东西,但都没有用。如何用javascript编写“数据”?

2 个解决方案

#1


3  

You need to post data in next format

您需要以下一种格式发布数据

{
"values[0][0]": "Some value",
"values[1][0]": "Some value",
"values[0][1]": "Some value",
"values[1][1]": "Some value",
"values[2][0]": "Some value",
"values[0][2]": "Some value",
"values[2][1]": "Some value",
...
}

#2


0  

Try with JSON.stringify.

与JSON.stringify试试。

var chiavi = [];
chiavi[0] = ["A", "B"];
chiavi[1] = ["C", "D"];

        $.ajax({
            type: "POST",
            url: jsonUrl, 
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            traditional: true,
            data: JSON.stringify({
                Values: chiavi
            }),
            success: function (result) {
            },
            error: function (response, textStatus, errorThrown) {
            }
        });

#1


3  

You need to post data in next format

您需要以下一种格式发布数据

{
"values[0][0]": "Some value",
"values[1][0]": "Some value",
"values[0][1]": "Some value",
"values[1][1]": "Some value",
"values[2][0]": "Some value",
"values[0][2]": "Some value",
"values[2][1]": "Some value",
...
}

#2


0  

Try with JSON.stringify.

与JSON.stringify试试。

var chiavi = [];
chiavi[0] = ["A", "B"];
chiavi[1] = ["C", "D"];

        $.ajax({
            type: "POST",
            url: jsonUrl, 
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            traditional: true,
            data: JSON.stringify({
                Values: chiavi
            }),
            success: function (result) {
            },
            error: function (response, textStatus, errorThrown) {
            }
        });