jQuery的AJAX参数没有被传递到MVC参数、jQuery、AJAX、MVC

2023-09-10 19:42:28 作者:少年、别太狂

我有点停留在什么可能是一个常见的​​情况,但无法找到太多的解决办法的途径。

I'm a bit stuck on what is probably a common situation, but can't find much in the way of solutions.

我通过单个int参数的MVC控制器的方法,期待一个JSON响应回来。但问题是,参数,同时被填充在客户端,不被认可的在服务器端,并正在PTED为空间$ P $。

I'm passing a single int parameter to an MVC controller method, expecting a Json response back. Problem is, the parameter, while being populated at the client end, is not being recognised at the server end and is being interpreted as null.

这里的code:

function getBatches(p) {
$.ajax({
    type: "GET",
    data: "{'ProjectID': " + p + "}",
    url: "/Home/Batches",
    success: function(msg) {
        populateBatches(msg);
    }
});

}

对于p的值是一个整数。在服务器端,code是这样的:

The value for p is an integer. On the server end, the code looks like this:

[AcceptVerbs(HttpVerbs.Get)]
public JsonResult Batches(int ProjectID)
{
    return Json(TimeHelper.GetBatchesForProject(ProjectID));
}

我试着改变它稍微使服务器端int参数为空(即诠释?ProjectID ),但同样,这似乎并没有帮助。问题是某处翻译。想法?

I've tried altering it slightly so that the server-side int argument is nullable (i.e. int? ProjectID) but again, that doesn't appear to help. The problem is somewhere in the translation. Ideas?

推荐答案

数据必须是一个JavaScript对象字面:

data needs to be a Javascript object literal:

$.ajax({
    type: "GET",
    data: {ProjectID: p},
    url: "/Home/Batches",
    success: function(msg) {
        populateBatches(msg);
    }
});