views:

39

answers:

1

I have the following action:

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

I'm calling it with the following jquery:

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

The parameters in my GetGridCell action are never filled (they are null). When debugging I can see that my Request.Form[0] is called longitude and has the correct value. Same goes for latitude.

When I use the exact same code, but with a $.get everything works fine.

What am I doing wrong?

A: 

Not too sure what you are doing wrong... Have you any route entry for 'Grid/GetGridCell'?

Try decorating your JsonResult method with the AcceptVerbs attribute, creating a separate one method for Get and another for Post

Under a quick test (for me) without any route entry I was able to pass the values:

Using the following for example to post the values:

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

using $.get intead calls

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

and

$.post calls

    [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);
    }
Nicholas Murray
My routing looks fine since I'm hitting the breakpoint in my action.I tried adding the HttpVerbs.Post attr and FormCollection par, but nothing. The formCollection does contain two keys with names exactly like the parameters and they contain the correct value..I also pasted your $.post (changed Home to Grid) but still nothingBizare
borisCallens
I think I've found the problem. When I don't use any decimal separators in the parameter I send from jquery, it works fine.I think it's the region settings that is the problem. I dev on my nl-BE machine where the decimal separator is a colon where jquery gives me a dot.
borisCallens
Cool, sounds like the region settings alright.
Nicholas Murray