gpt4 book ai didi

asp.net-mvc - 用于 json 嵌套数组的 MVC3 绑定(bind)

转载 作者:行者123 更新时间:2023-12-05 00:34:43 26 4
gpt4 key购买 nike

我有一个简单的 javascript 数组。它是这样声明的:

coords = []

每次用户单击图像时,我都会执行以下操作:
coords.push([x,y])

所以我最终得到了这样的东西:(在javascript中)
[[342,144],[477,99],[632,148],[529,162]]

但我不知道在 Controller 方法中将它绑定(bind)到什么......我试过了
List<List<int>>, int[][], int[,]

它们似乎都不起作用。它仅在我使用字符串时有效。

这是我用来将其发送到服务器的代码:
$.ajax({
type: "POST",
url: "/home/SaveCoords",
data: { coords: JSON.stringify(coords) }
}).done(function (msg) {
alert("Data Saved: " + msg);
});

这是我在 Controller 上使用的代码
[HttpPost]
public ActionResult SaveCoords(string coords)
{
return Json("Hello", JsonRequestBehavior.AllowGet);
}

帮助?

最佳答案

您应该将请求内容类型设置为 application/json , 并更改 data因此。

这是工作示例:

    $.ajax({
type: "POST",
url: "/home/SaveCoords",
contentType : 'application/json',
data: JSON.stringify(coords)
}).done(function (msg) {
alert("Data Saved: " + msg);
});

和服务器
    public ActionResult SaveCoords(int[][] coords)
{
return View();
}

关于asp.net-mvc - 用于 json 嵌套数组的 MVC3 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10354390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com