我正在使用jquery插件Jcrop进行自定义图像大小调整,在将参数传递给方法调用时,将数据传递给MVC时,MVC将正确解释数据,这是一个问题。
下面是我的Ajax调用
var cropArea = { X: 50, Y: 50, Width: 200, Height: 200 };
var destSize = { Width: 200, Height: 200 };
$.ajax ({
url: "image/CropImage",
type: "POST",
data: JSON.stringify({ id: image.id, cropArea: cropArea, size: destSize }),
dataType: "json"
});
C#代码如下所示
[HttpPost]
public ActionResult ResizeImage(int id, Rectangle cropArea, Size size)
{
// Crop Code here
}
Id可以正常使用,但是Rectangle和size对象就不行了,我只得到了cropArea和Size的空异常错误。我可以将矩形和大小参数作为数组传入,但我更愿意将矩形和大小对象传入
发布于 2013-06-26 18:58:40
您的jquery ajax代码缺少contentType
参数:
var cropArea = { X: 50, Y: 50, Width: 200, Height: 200 };
var cropArea = { X: 50, Y: 50, Width: 200, Height: 200 };
var destSize = { Width: 200, Height: 200 };
$.ajax({
url: "image/CropImage",
type: "POST",
data: JSON.stringify({ id: image.id, cropArea: cropArea, size: destSize }),
dataType: "json",
contentType: 'application/json; charset=utf-8'
});
我已经在我的家庭控制器中使用了一个虚拟方法和一些虚拟类来快速测试:
[HttpPost]
public ActionResult ResizeImage(int id, Rectangle cropArea, Size size)
{
// Crop Code here
return Json(true);
}
public class Rectangle
{
public int X { get; set; }
public int Y { get; set; }
public int Width { get; set; }
public int Height { get; set; }
}
public class Size
{
public int Width { get; set; }
public int Height { get; set; }
}
当我点击一个按钮时,用下面的代码发布:
$("#test").click(function () {
var cropArea = { X: 50, Y: 50, Width: 200, Height: 200 };
var destSize = { Width: 200, Height: 200 };
$.ajax({
url: "/Home/ResizeImage",
type: "POST",
data: JSON.stringify({ id: 210, cropArea: cropArea, size: destSize }),
dataType: "json",
contentType: 'application/json; charset=utf-8'
});
});
希望它能帮上忙!
https://stackoverflow.com/questions/17325789
复制