jqGrid和MVC3 url动作方法没有被调用

时间:2012-02-20 12:16:28

标签: asp.net-mvc-3 jqgrid

我无法让jqGrid在我的控制器上调用我的动作方法。我对此完全陌生,所以我可能会犯很多新手错误。我从jqGrids文档中获取了samplecode并对其进行了一些修改。

视图中的代码:

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

我在我的控制器动作方法中设置了断点,但我无法让它击中。

1 个答案:

答案 0 :(得分:3)

必须将参数调用_search,而不是search。此外,您似乎没有传递任何JSON数据。这是一个例子:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult GetContactRows(string sidx, string sord, int page, int rows, bool _search, string filters)
    {
        var data = new
        {
            page = 1,
            total = 1,
            records = 3,
            rows = new[]
            {
                new 
                {
                    id = 1,
                    cell = new[] { "Name 1", "Address 1", "City 1" },
                },
                new 
                {
                    id = 2,
                    cell = new[] { "Name 2", "Address 2", "City 2" },
                },
                new 
                {
                    id = 3,
                    cell = new[] { "Name 3", "Address 3", "City 3" },
                }
            }
        };
        return Json(data, JsonRequestBehavior.AllowGet);
    }
}

注意使用_search作为参数名称的操作签名,并注意我们在返回JSON时使用JsonRequestBehavior.AllowGet允许GET请求。

和视图:

<script src="@Url.Content("~/Scripts/jqgrid/js/jquery.jqGrid.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    $(function () {
        $("#list").jqGrid({
            url: '@Url.Action("GetContactRows", "Home")',
            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'
        });
    }); 
</script>

<table id="list"></table>

<rant>

另外,为了能够轻松调试这些问题,请使用一个javascript调试工具,如FireBug。它允许您查看浏览器中的所有AJAX请求以及服务器作为响应发送的内容。如果你这样做了,你会立即看到这个错误。

我花了70秒,去了jqGrid网站,下载它,创建一个新的ASP.NET MVC 3应用程序,复制粘贴代码,引用我之前下载的jqGrid,点击F5运行应用程序,在FireFox中点击F12打开FireBug,看到jqGrid尝试执行的AJAX请求显示为红色(因为服务器返回了HTTP 500状态代码),单击此AJAX请求旁边的+号并读取确切的异常服务器发送的消息告诉我它找不到search参数的值,该参数是一个不可为空的布尔值,单击Params选项卡查看客户端发送的确切参数:

enter image description here

当您使用正确的工具时,您是否觉得进行Web开发有多么简单? 70秒它比在StackOverflow上提问更快。

</rant>

相关问题