1

カスタム画像のサイズ変更に jquery プラグイン Jcrop を使用していますが、引数をメソッド呼び出しに渡すときにデータを正しく解釈する方法でデータを 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 とサイズの null 例外エラーが発生します。長方形とサイズのパラメーターを配列として渡すことができますが、長方形とサイズのオブジェクトを渡したほうがよいでしょう

4

1 に答える 1

0

contentTypejquery ajax コードにパラメータがありません:

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'
    });
});

それが役に立てば幸い!

于 2013-06-26T18:58:40.360 に答える