我有一个简单的
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(); }