jqGrid 和 MVC3 url 操作方法不会被调用

发布于 01-06 21:59 字数 1042 浏览 1 评论 0原文

我无法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青柠芒果2025-01-13 21:59:55

该参数必须名为 _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>

另外,为了能够轻松调试此类问题,请使用 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, not search. Also you don't seem to be passing any JSON data. Here's an example:

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

Notice the action signature using _search as parameter name and also notice that we allow GET requests by using JsonRequestBehavior.AllowGet when returning the JSON.

and the view:

<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>

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 the Params tab and see the exact parameters sent by the client:

enter image description here

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>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文