【问题标题】:Action Class not getting called when doing Search through JQ Grid通过 JQGrid 进行搜索时未调用操作类
【发布时间】:2011-04-27 10:12:12
【问题描述】:

我正在使用 MVC 2.0 Web 应用程序,其中很少有按钮的 onclick 我调用操作方法,但是当我第一次按下搜索按钮时,操作方法被调用,但之后如果我尝试单击搜索按钮没有调用 action 方法。

另外让我告诉你,我正在使用 JQ Grid 来显示按钮 onclick 上的数据。 请看下面的代码sn-p。

函数加载网格(数据,类型){

        var showGrid = $("#showGrid");
        var navigation = $("#navigation");

        showGrid.jqGrid({
            url: '/Customer/CustomerSearchInfo',
            datatype: 'json',
            mtype: 'POST',
            cache: false,
            postData: { param: data, type: type },

            colNames: ['Customer Name', 'Contact Name', 'Company Number', 'Customer Number', 'Link Number', 'Phone', 'SalesRep Name', 'Sequence'],
            colModel: [
              { name: 'COMPANY_NAME', index: '1', align: 'left', sortable: true },
              { name: 'CONTACT_NAME', index: '2', align: 'left', sortable: true },
              { name: 'COMPANY_NUM', index: '3', align: 'left', sortable: true },
              { name: 'CUSTOMER_NUM', index: '4', align: 'left', sortable: true },
              { name: 'LINK_NUM', index: '5', align: 'left', sortable: true },
              { name: 'PHONE_1', index: '6', align: 'left', sortable: true },
              { name: 'SALESREP_NUM', index: '7', align: 'left', sortable: true },
              { name: 'ADDRESS_SEQ_NUM', index: '8', align: 'left', sortable: true }
             ],
            pager: navigation,
            rowNum: 10,
            rowList: [5, 10, 20, 30, 50],
            viewrecords: true,
            caption: '',
            height: '250px',
            sortorder: 'asc',
            sortname: '0',
            shrinkToFit: true,
            autowidth: true,
             }
        })

    }; 

提到的操作方法 (/Customer/CustomerSearchInfo) 没有被第二次调用。

[AcceptVerbs(HttpVerbs.Post)] public ActionResult CustomerSearchInfo(string param, int type) {

        try
        {
            var custInfo = new List<Customer>();
            switch (type)
            {
                case 1:
                    custInfo = custDO.GetCustomerInfo(customerID: param);
                    break;
                case 2:
                    custInfo = custDO.GetCustomerInfo(customerName: param);
                    break;
                case 3:
                    custInfo = custDO.GetCustomerInfo(contactName: param);
                    break;
                case 4:
                    custInfo = custDO.GetCustomerInfo(companyID: param);
                    break;
                case 5:
                    custInfo = custDO.GetCustomerInfo(salesRepID: param);
                    break;
                case 6:
                    custInfo = custDO.GetCustomerInfo(phone: param);
                    break;
                case 7:
                    custInfo = custDO.GetCustomerInfo(addrtype: param);
                    break;
                case 8:
                    custInfo = custDO.GetCustomerInfo(status: param);
                    break;
                case 9:
                    custInfo = custDO.GetCustomerInfo(linkID: param);
                    break;
            }

                            return custInfo != null
                       ? Json(GetJson(custInfo, 10, custInfo.Count, 0),
                              JsonRequestBehavior.DenyGet)
                       : Json(null, JsonRequestBehavior.DenyGet);
        }
        catch (Exception ex)
        {
            Response.StatusCode = 500;
            return Json(null, JsonRequestBehavior.DenyGet);
        }


    }

以下是 2 个搜索按钮供参考:

input id="btnCustID" type="button" class="buttonsearch" title="按客户 ID 搜索" onclick="LoadGrid(document.getElementById('txtCustID').value,1)"

input id="btnCustName" type="button" class="buttonsearch" title="按客户名称搜索" onclick="LoadGrid(document.getElementById('txtCustName').value,2)"

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您的 JavaScript 将无法工作,因为您正在尝试重新初始化已经初始化的 jqGrid。您可以做的事情很少。

    您可以在再次初始化之前卸载您的 jqGrid(下载网格时需要标记自定义复选框):

    var showGrid = $("#showGrid");
    var navigation = $("#navigation");
    
    showGrid.jqGrid('GridUnload');
    ...
    

    您可以只更改 postData 并重新加载您的 jqGrid 而无需重新初始化(您需要提前初始化它):

    var showGrid = $("#showGrid");
    showGrid.setPostData({ param: data, type: type });         
    showGrid.jqGrid('setGridParam', { page: 1 }).trigger("reloadGrid");
    

    或者你可以实现原生的jqGrid搜索,这里有一些简单的描述:http://tpeczek.com/2009/11/jqgrid-and-aspnet-mvc-searching.html

    【讨论】:

    • 感谢 tpeczek 它解决了我的问题,但还有一个问题是,虽然第二次搜索数据得到反映,但我希望它应该清除文本框中以前的值(搜索字段)也存在于该页面中。是否有 jq 网格的任何属性可以做到这一点,或者我们是否需要为每个存在的文本框单独清除文本框值。
    • 在设置 jqgrid postData: $('#txtCustID').val(''); $('#txtCustName').val('');
    【解决方案2】:

    我建议你安装Fiddler 看看到底发生了什么;对于初学者:是否第二次提出请求?它是什么样子的?

    【讨论】:

    • 是的,第二次它没有调用动作类,即使在调试模式下也没有被命中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多