This modelbinding from jquery, what's wrong with it?
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?
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);
}
精彩评论