C# 这是来自jquery的modelbinding,what';怎么了?
我有以下行动:C# 这是来自jquery的modelbinding,what';怎么了?,c#,.net,jquery,asp.net-mvc-2,model-binding,C#,.net,Jquery,Asp.net Mvc 2,Model Binding,我有以下行动: 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调用它:
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]被称为longitude,并且具有正确的值。纬度也是如此
当我使用完全相同的代码,但使用$.get
时,一切正常
我做错了什么?不太确定你做错了什么。。。您有“Grid/GetGridCell”的路由条目吗 尝试使用AcceptVerbs属性装饰JsonResult方法,为Get和Post分别创建一个方法 在没有任何路线输入的快速测试(对我而言)下,我能够通过以下值:
$.post('Home/GetGridCell', { longitude: 11.6, latitude: 22.2 },
function(data) {
alert(data);
});
使用以下示例发布值:
$.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);
}
及
$电话留言
[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);
}
我的路由看起来不错,因为我在操作中遇到了断点。我试着添加了HypPosith.Pad和Frand集合PAR,但没有。formCollection确实包含两个名称与参数完全相同的键,并且它们包含正确的值。。我还粘贴了你的$.post(将Home改为Grid),但仍然没有任何问题。我想我已经找到了问题所在。当我在从jquery发送的参数中不使用任何十进制分隔符时,它可以正常工作。我认为这是地区设置的问题。我在NLBE机器上开发了一个程序,其中十进制分隔符是一个冒号,jquery给我一个点。