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

我有一个简单的 javascript数组.它的声明如下:

coords = []

每次用户点击图片时,我都会这样做:

coords.push([x,y])

所以我最终得到这样的东西:(在javascript中)

[[342,144],[477,99],[632,148],[529,162]]

但我不知道在控制器方法中将它绑定到什么…我试过了

List<List<int>>,int[][],int[,]

他们似乎都没有工作.它只在我使用字符串时才有效.

这是我用来将它发送到服务器的代码

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

这是我在控制器上使用的代码

[HttpPost]
public ActionResult SaveCoords(string coords)
{
    return Json("Hello",JsonRequestBehavior.AllowGet);
}

救命?

解决方法

您应该将请求内容类型设置为application / json,并相应地更改数据.

这是工作示例:

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

和服务器

public ActionResult SaveCoords(int[][] coords)
    {
        return View();
    }

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....