JQuery Grid PostData没有将值传递给控制器​​操作方法 - MVC3

时间:2012-04-12 11:19:52

标签: jquery asp.net-mvc-3

我正在使用JQuery网格在页面上显示一些数据。我有一个复选框列表,用于将滤镜应用到网格,我需要传递给控制器​​操作方法。我的问题是,尽管在传递发布数据之前正确检索了所选复选框的值,但它并未显示在控制器中。相反,如果我正在加载选中了复选框的页面,那么该值将被传递。我很困惑为什么会这样。我会非常乐意为你提供任何帮助。请参阅下面的代码我用来绑定网格:

    function BindGrid() {

        var selectedPriority = [];
        $('#priority input:checked').each(function () {
            selectedPriority.push($(this).val());
        });
        var selectedState = [];
        $('#state input:checked').each(function () {
            selectedState.push($(this).val());
        });
        var selectedAckState = [];
        $('#ackState input:checked').each(function () {
            selectedAckState.push($(this).val());
        });

        var x = convertIntArrayToString(selectedPriority);
        var y = convertIntArrayToString(selectedState);
        var z = convertIntArrayToString(selectedAckState);

        jqDataUrl = "Alarm/LoadjqData";
        // Set up the jquery grid
        //$("#jqTable").jqGrid('clearGridData');
        $("#jqTable").jqGrid({
            url: jqDataUrl,
            datatype: "json",
            mtype: "POST",
            postData: {
                sp: x,
                ss: y,
                sak: z
            },
            cache: false,
            // Specify the column names
            colNames: ["Id", "PointRef", "Value", "AlarmStateName", "AckStateName", "Priority", "AlarmDate"],

            // Configure the columns
            colModel: [
                { name: "AlarmId", index: "AlarmId", width: 70, align: "left" },
                { name: "PointRef", index: "PointRef", width: 200, align: "left" },
                { name: "Value", index: "Value", width: 200, align: "left" },
                { name: "AlarmStateName", index: "AlarmStateName", width: 150, align: "left" },
                { name: "AcknowledgementStateName", index: "AcknowledgementStateName", width: 170, align: "left" },
                { name: "Priority", index: "Priority", width: 100, align: "left" },
                { name: "AlarmDate", index: "AlarmDate", width: 250, align: "left" }
                ],

            // Grid total width and height
            width: 700,
            height: 250,

            // Paging
            toppager: true,
            pager: $("#jqTablePager"),
            rowNum: 10,
            rowList: [5, 10, 20],
            viewrecords: true, // Specify if "total number of records" is displayed

            // Default sorting
            sortname: "AlarmId",
            sortorder: "desc",

            // Grid caption
            caption: "Telemetry Alarms"
        }).navGrid("#jqTablePager",
                { refresh: true, add: false, edit: false, del: false },
                    {}, // settings for edit
                    {}, // settings for add
                    {}, // settings for delete
                    {sopt: ["cn"]} // Search options. Some options can be set on column level
             )
        //.trigger('reloadGrid', [{ page: 1}]); 
             .trigger('reloadGrid');

    }

    function convertIntArrayToString(data) {
        var x='';
        //alert(data.length);
         for(var i=0;i<data.length;i++) {
          x = x + data[i] + ",";
      }
      if (x != '') {
          x = x.substr(0, x.length - 1);
          //alert(x);
      }
      return x.toString();
    }

//Controller action method

 [HttpPost]
        public ActionResult LoadjqData(string sidx, string sord, int page, int rows, bool _search, string searchField, string searchOper,
            string searchString, string sp, string ss, string sak)
        {

            //code ommited.
            return Json(jsonData);
        }

sp,ss和sak参数的值在控制器中没有显示。

1 个答案:

答案 0 :(得分:0)

这篇文章解决了我的问题:

jqgrid not updating data on reload

我不得不将用于读取复选框值的代码移动到一个单独的函数中,并在PostData中访问该函数,它就像一个魅力。没有它,它每次都发送相同的数据。

更新后的代码如下:

function BindGrid() {
    var jqDataUrl = null;
    jqDataUrl = "Alarm/LoadjqData" //+ x;
    $("#jqTable").jqGrid({
        url: jqDataUrl,
        cache: false,
        datatype: "json",
        mtype: "POST",
        postData: {
            sp: function () { return getPriority(); }
        },
        // Specify the column names
        colNames: ["Id", "PointRef", "Value", "AlarmStateName", "AckStateName", "Priority", "AlarmDate"],

        // Configure the columns
        colModel: [
            { name: "AlarmId", index: "AlarmId", width: 70, align: "left" },
            { name: "PointRef", index: "PointRef", width: 200, align: "left" },
            { name: "Value", index: "Value", width: 200, align: "left" },
            { name: "AlarmStateName", index: "AlarmStateName", width: 150, align: "left" },
            { name: "AcknowledgementStateName", index: "AcknowledgementStateName", width: 170, align: "left" },
            { name: "Priority", index: "Priority", width: 100, align: "left" },
            { name: "AlarmDate", index: "AlarmDate", width: 250, align: "left" }
            ],

        // Grid total width and height
        width: 700,
        height: 250,

        // Paging
        toppager: true,
        pager: $("#jqTablePager"),
        rowNum: 10,
        rowList: [5, 10, 20],
        viewrecords: true, // Specify if "total number of records" is displayed

        // Default sorting
        sortname: "AlarmId",
        sortorder: "desc",

        // Grid caption
        caption: "Telemetry Alarms"
    }).navGrid("#jqTablePager",
            { refresh: true, add: false, edit: false, del: false },
                {}, // settings for edit
                {}, // settings for add
                {}, // settings for delete
                {sopt: ["cn"]} // Search options. Some options can be set on column level
         )
         .trigger('reloadGrid');

}

function convertIntArrayToString(data) {
    var x = '';
    //alert(data.length);
    for (var i = 0; i < data.length; i++) {
        x = x + data[i] + ",";
    }
    if (x != '') {
        x = x.substr(0, x.length - 1);
        //alert(x);
    }
    return x.toString();
}

function getPriority()
{
 var selectedPriority = [];
    $('#priority input:checked').each(function () {
        selectedPriority.push($(this).val());
    });
    return convertIntArrayToString(selectedPriority);

}