jQGrid setGridParam函数不能与MVC Action一起使用

时间:2014-03-11 04:20:33

标签: javascript jquery asp.net-mvc jqgrid

我尝试使用日期字段过滤 jQgrid 数据。请看下面的图片。

enter image description here

我的网页上有两个文本框和一个按钮。我尝试过滤客户数据

在所选日期范围之间。我在按钮点击事件中调用了我的setGridParam。

请参阅下面的HTML。

 $("#Button1").click(function () {

    var fromdate = $("#txtFrom").val();
    var todate = $("#txtTo").val();

    jQuery("#jQGridDemo").jqGrid('setGridParam', {
        url: "/Home/GetFilterData?sidx=" + fromdate + "&sord=" + todate, page: 1
    }).trigger("reloadGrid");

});

这是我的控制器动作

    [HttpPost]
    public JsonResult GetFilterData(string sidx, string sord)
    {
        using (jQGridDemoEntities db = new jQGridDemoEntities())
        {
            var customers = new List<Customer>();

            customers = db.Customers.ToList();

            return Json((
            from customer in customers
            orderby customer.Id descending
            select new[]{
                    customer.Id.ToString(CultureInfo.InvariantCulture),
                    customer.FirstName,
                    customer.LastName,
                    customer.IsMale.ToString(),
                    customer.Address,
                    customer.Email,
                    customer.Phone,
                    customer.Country.Name,
                    customer.Note,
                    customer.Created.ToString()
                }).ToArray(), JsonRequestBehavior.AllowGet);
        }
    }

我在SetGridParam函数中调用此函数,但不会触发此操作。

请帮助。

1 个答案:

答案 0 :(得分:3)

参数sidxsord是根据选项sortnamesortorder动态构建的。因此,如果您确实需要设置参数,则应将setGridParam与包含sortnamesortorder属性的对象一起使用。

您使用fromdatetodate作为sidxsord的值。因此我怀疑您只需要向服务器发送一些附加参数,并尝试使用现有参数。这不是最好的方式。我建议您介绍其他参数fromDatetoDate,并使用postData参数作为jqGrid选项:

// create jqGrid with additional postData parameter
$("#jQGridDemo").jqGrid({
    url: "/Home/GetFilterData",
    postData: {
        fromDate: function () {
            return $("#txtFrom").val();
        },
        toDate: function () {
            return $("#txtTo").val();
        }
    },
    ...
});

$("#Button1").click(function () {
    $("#jQGridDemo").trigger("reloadGrid", [{page: 1}]);
});

您还需要更改GetFilterData操作的参数名称,这些参数对应于postData的属性名称:

public JsonResult GetFilterData(string fromDate, string toDate)
{
    ...
}

我建议您阅读the answerthis one以获取更多信息。

相关问题