0

次のアクションがあります。

public JsonResult GetGridCell(double longitude, double latitude)
{
    var cell = new GridCellViewModel { X = (int)Math.Round(longitude.Value, 0), Y = (int)Math.Round(latitude.Value, 0) };
    return Json(cell);             
}

次のjqueryで呼び出しています:

$.post('Grid/GetGridCell', { longitude: location.longitude, latitude: location.latitude },
    function (data) {
        InsertGridCellInfo(data);
    });

私の GetGridCell アクションのパラメーターは入力されません (null です)。デバッグすると、 Request.Form[0] が経度と呼ばれ、正しい値を持っていることがわかります。緯度についても同様です。

まったく同じコードを使用すると、$.getすべて正常に動作します。

私は何を間違っていますか?

4

1 に答える 1

0

何が間違っているのかよくわかりません...「Grid/GetGridCell」のルートエントリはありますか?

JsonResult メソッドを AcceptVerbs 属性で装飾し、Get 用と Post 用に別のメソッドを作成してみてください。

ルートエントリなしの簡単なテスト(私にとって)では、値を渡すことができました:

たとえば、次を使用して値を投稿します。

$.post('Home/GetGridCell', { longitude: 11.6, latitude: 22.2 },
function(data) {
    alert(data);
});

$.get intead 呼び出しの使用

    [AcceptVerbs(HttpVerbs.Get)]
    public JsonResult GetGridCell(double longitude, double latitude)
    {
        var cell = new GridCellViewModel { X = (int)Math.Round(longitude), Y = (int)Math.Round(latitude) };
        return Json(cell);
    }

$.post 呼び出し

    [AcceptVerbs(HttpVerbs.Post)]
    public JsonResult GetGridCell(double longitude, double latitude, FormCollection collection)
    {
        var cell = new GridCellViewModel { X = (int)Math.Round(longitude), Y = (int)Math.Round(latitude) };
        return Json(cell);
    }
于 2010-06-28T23:21:35.880 に答える