【问题标题】:Controlling the JqGrid Parameters While Making an Ajax Post in MVC2在 MVC2 中进行 Ajax 发布时控制 JqGrid 参数
【发布时间】:2012-04-10 15:39:45
【问题描述】:

我对在 MVC2 中处理 ajax 发布请求的指南感到困惑 我如何知道网格将哪些数据作为请求参数传递?以什么顺序和什么数据类型?因为知道这一点后,只有一个人可以设计服务器端的post方法。我已经看到了许多使用不同函数原型作为服务器端方法处理程序的示例。

这怎么可能?我的意思是它与做 ajax 发布的 jqgrid 相同。同一个 jqgrid 怎么会有不同类型的函数原型作为服务器端动作?

编辑

我的要求是,当 JqGrid 进行 ajax 调用时,我想发送一些额外的数据,例如下拉列表选择值。但是 MVC 只接受 JqGrid 参数。虽然我通过“paramData”添加了额外的数据并且我能够在控制器请求处理程序中接收它,但我有一个解决方法。问题是我们使用了一个 Grid 类来反序列化 Grid 参数,而这个类对应用程序来说是全局的。所以为每一页修改它是不行的。

我需要的是这不起作用,只填充第一个参数:-

public void Jgrid(Jgrid grid,object hdnupdpg,string p_roleid)
{
}

但是我如何让 Jgrid.ajax 调用发送这些其他参数?仅使用“paramsData”选项?

这是我遇到的服务器端函数原型:

public void JGridData(JGrid grid)
{

}

这里是网格类

    public class JGrid
    {
        private bool IsSearch;
        public string sidx { get; set; }
        public string sord { get; set; }
        public int page { get; set; }
        public int rows { get; set; }

        public bool _search
        {
            get
            {
                string strSearch = HttpContext.Current.Request["_search"];
                if (!string.IsNullOrEmpty(strSearch))
                    return Convert.ToBoolean(strSearch);
                else
                    return IsSearch;
            }
            set { IsSearch = value; }
        }

        public string searchOper { get; set; }
        public string filters { get; set; }
        public int totalRecords { get; set; }
        public string procName { get; set; }
        public string SearchValue { get; set; }
        public string SearchField { get; set; }

        public string defaultFilter { get; set; }

        public string SortExpression
        {
            get { return sidx + " " + sord; }
        }


        public string FilterExpression
        {
            get
            {
                string filter = BuildFilter();
                if (!string.IsNullOrEmpty(defaultFilter) && !string.IsNullOrEmpty(filter))
                    return defaultFilter
                           + " AND (" + filter + ")";
                else if (!string.IsNullOrEmpty(defaultFilter))
                    return defaultFilter;
                return filter;
            }
        }

        public string BuildFilter()
        {
         ....
        }
}

编辑

Here is my Script for JqGrid

    jQuery('#jgrid').jqGrid({
        autowidth: true,
        altRows: true,
        altclass: 'grdAltRwClr',
        datatype: 'local',
        forceFit: true,
        gridview: true,
        height: 290,
        mtype: 'post',
        rowList: [10, 20, 30],
        rowNum: 10,
        pager: '#pager',
        pagerpos: 'right',
        recordpos: 'left',
        rownumbers: false,
        scrollrows: false,
        sortname: 'roledtlid',
        toolbar: [true, "top"],
        url: rootPath + 'RoleDetail/JGridData',
        postData: { extraparams: function() { return escape(jQuery('#hdnupdpg').val()); },
        parentid: function() { return escape(jQuery('#p_roleid').val()); }
         },
        beforeSelectRow: function(rowid, e) { return false; },
        gridComplete: function() { GridComplete() },
        colModel: [
              { name: 'act', label: 'View', resizable: false, search: false, sortable: false, title: false, width: 6, index: 'act' }
            , { name: 'roleid', label: 'Role id', width: 10, index: 'roleid' }
            , { name: 'rolename', label: 'Role Name', width: 25, index: 'rolename' }
            , { name: 'pgname', label: 'Page Name', width: 30, index: 'pgname' }
            , { name: 'canedit', label: 'Edit', width: 10, index: 'canedit' }
            , { name: 'canview', label: 'View', width: 10, index: 'canview' }
             ]
    });

【问题讨论】:

    标签: jquery asp.net-mvc jqgrid


    【解决方案1】:

    将发送到服务器的参数列表取决于您使用的选项。您没有发布您使用的 javaScript 代码。您可以根据 jqGrid 的 prmNames 选项重新定义的任何参数的名称。以下参数将始终发送到用于填充网格的 URL

    • page - 请求的页面 - 默认值page,
    • rows - 请求的行数 - 默认值 rows,
    • sort - 排序列 - 默认值sidx,
    • order - 排序顺序默认值sord,
    • search - 搜索指示器 - 默认值 _search

    如果您将Advanced Searching dialogfilter toolber 与参数stringResult: true 一起使用,则有关过滤器的信息将以here 所述格式在附加参数filters 中发送。

    例如,如果您在服务器响应的标头中设置Cache-Control: private, max-age=0(请参阅herehere)或使用服务器响应的其他Cache-Control 参数控制缓存,您可以删除nd 参数包含时间戳:

    prmNames: { nd: null }
    

    如果你想将 _search 参数重命名为 isSearch 例如你可以使用

    prmNames: { search: 'isSearch' }
    

    您当然可以组合您需要的所有设置:

    prmNames: { nd: null, search: 'isSearch' }
    

    【讨论】:

    • 因此,如果我必须通过 paramsdata 发送额外的自定义参数,我是否必须更改我的网格类?有什么方法可以在我的 MVC 后处理程序中为 JqGrid ajax 请求使用两个或多个参数?如果是这样,我如何配置网格的 ajax 调用来发送这些额外的参数?我不能继续为每个页面更改我的 Grid 类。
    • @Deb:对不起,我不明白你想如何在服务器端使用“额外的自定义参数”。所以很难评论您使用的 Grid 类的定义。你试过只使用public void JGridData(JGrid grid, string extraparams, string parentid)吗?顺便说一句,postData 内部的escape 函数的使用是不需要的。数据将由$.ajax 发送,内部调用$.param,使用encodeURIComponent
    • 假设我想根据一些下拉列表值填充网格,该值是我的存储过程的参数,在这种情况下,我需要发送常规 JQuery 网格参数以及下拉列表选择值。对于常见的 Jgrid 参数,我使用的是 Jgrid 类,但对于下拉值,我无法控制 jqGrid 正在调用的 ajax 调用。我可以编辑我的 JGrid 类,但它在每个控制器中都使用,因此不能为每个页面修改它。这就是辩证法......
    • @Deb:首先你应该在你的问题后面加上额外的信息,而不是修改全文或代码。您应该考虑该问题的其他读者。如果有人现在会阅读我的问题,那么很多事情都不清楚。现在到你的问题。我真的不明白。无论如何,这不是 jqGrid 问题。 jqGrid 可以发送您需要的参数,例如来自#hdnupdpg#p_roleid 控件的值。在服务器端,您必须使用相同名称的参数。如果你使用extraparams 代表#hdnupdpgparentid 代表#p_roleid 你应该...
    • @Deb:您应该使用public void Jgrid(Jgrid grid, string extraparams, string parentid) 而不是public void JGridData (Jgrid grid,object hdnupdpg,string p_roleid)。您还可以使用public void JGridData (int page, int rows, string sidx, string sord, bool _search, string extraparams, string parentid) 之类的原型,并在JGridData 正文中使用Jgrid 构造函数。
    【解决方案2】:

    不清楚你对jqgrid的哪种方法感兴趣。但让我试试

    一般情况下 - 使用 Firebug for Firefox 可以看到您感兴趣的请求参数

    • 在 Firefox 中安装 Firebug 插件
    • 使用 jqgrid 打开页面/如果您还没有 jqgrid demo 页面,请打开页面
    • 从 Firefox 右上角激活 Firebug 控制台
    • 在 Firebug 的“网络”选项卡中观察请求响应
    • 如果您展开请求,它将显示在该特定请求中发送的所有参数和标头信息

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-01
      • 2013-07-22
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      相关资源
      最近更新 更多