jqGrid 和 MVC3 url 操作方法不会被调用
我无法让 jqGrid 调用控制器上的操作方法。我对此完全陌生,所以我可能犯了很多新手错误。我从 jqGrids 文档中获取了示例代码并对其进行了一些修改。
视图中的代码:
$(function () {
$("#list").jqGrid({
url: '@Url.Action("GetContactRows", "Contact")',
datatype: 'json',
mtype: 'GET',
colNames: ['Name', 'Address', 'City'],
colModel: [
{ name: 'Name', index: 'Name', width: 80 },
{ name: 'Address', index: 'Address', width: 80 },
{ name: 'City', index: 'City', width: 80 }
],
pager: '#pager',
rowNum: 10,
rowList: [10, 20, 30],
sortname: 'invid',
sortorder: 'desc',
viewrecords: true,
gridview: true,
caption: 'List of Contacts'
});
});
控制器中的代码:
public JsonResult GetContactRows(string sidx, string sord, int page, int rows, bool search, string filters)
{
System.Diagnostics.Debug.WriteLine("asdf");
return new JsonResult();
}
我在控制器操作方法中设置了一个断点,但我就是无法让它命中。
I can't get the jqGrid to call my action method on my controller. I'm completely new to this, so I'm probably making a lot of newbie mistakes. I've taken samplecode from jqGrids documentation and modified it a bit.
Code in the view:
$(function () {
$("#list").jqGrid({
url: '@Url.Action("GetContactRows", "Contact")',
datatype: 'json',
mtype: 'GET',
colNames: ['Name', 'Address', 'City'],
colModel: [
{ name: 'Name', index: 'Name', width: 80 },
{ name: 'Address', index: 'Address', width: 80 },
{ name: 'City', index: 'City', width: 80 }
],
pager: '#pager',
rowNum: 10,
rowList: [10, 20, 30],
sortname: 'invid',
sortorder: 'desc',
viewrecords: true,
gridview: true,
caption: 'List of Contacts'
});
});
Code in the controller:
public JsonResult GetContactRows(string sidx, string sord, int page, int rows, bool search, string filters)
{
System.Diagnostics.Debug.WriteLine("asdf");
return new JsonResult();
}
I've set a breakpoint in my controller action method, but I just can't get it to hit.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

该参数必须名为
_search
,而不是search
。另外,您似乎没有传递任何 JSON 数据。下面是一个示例:请注意使用
_search
作为参数名称的操作签名,还要注意我们在返回 JSON 时使用JsonRequestBehavior.AllowGet
允许 GET 请求。和视图:
另外,为了能够轻松调试此类问题,请使用 JavaScript 调试工具,例如 FireBug。它允许您查看浏览器中的所有 AJAX 请求以及服务器发送的响应内容。如果您这样做了,您会立即看到此错误。
我花了整整 70 秒,转到 jqGrid 网站,下载它,创建一个新的 ASP.NET MVC 3 应用程序,复制粘贴您的代码,引用我之前下载的 jqGrid,按 F5 运行该应用程序,按在FireFox中F12打开FireBug,看到jqGrid尝试执行的AJAX请求显示为红色(因为服务器返回了HTTP 500状态码),点击这个AJAX旁边的+号请求并读取服务器发送的确切异常消息,告诉我它找不到
search
参数的值(该参数是一个不可为空的布尔值),单击Params
选项卡并查看客户端发送的确切参数:当您使用正确的工具? 70秒。这比在 StackOverflow 上提问要快。
The parameter must be called
_search
, notsearch
. Also you don't seem to be passing any JSON data. Here's an example:Notice the action signature using
_search
as parameter name and also notice that we allow GET requests by usingJsonRequestBehavior.AllowGet
when returning the JSON.and the view:
<rant>
Also to be able to easily debug those kind of problems please use a javascript debugging tool such as FireBug. It allows you to see all AJAX requests in your browser as well as what the server sends as response. If you had done that you would have immediately seen this error.
It took me exactly 70 seconds, to go to the jqGrid web site, download it, create a new ASP.NET MVC 3 application, copy-paste your code, reference jqGrid that I have previously downloaded, hit F5 to run the application, hit F12 in FireFox to open FireBug, see that the AJAX request that jqGrid attempted to perform was shown in red (because the server returned HTTP 500 status code), click on the + sign next to this AJAX request and read the exact exception message sent by the server telling me that it cannot find a value for the
search
parameter which is a non nullable boolean, click on theParams
tab and see the exact parameters sent by the client:Do you see how trivially easy is to do web development when you use the right tools? 70 seconds. It's faster than asking a question on StackOverflow.
</rant>