0

我是 jqGrid 的新手。我现在的任务是在具有 jqGrid 的 View (asp.net MVC3) 中显示 TotalDiabetes,所以我选择了 userData。我的 jqGrid 控制器代码如下,

 extraPersons = ExtraPersonService.GetAllExtraPersons();
        int TotalDiabetesCount = extraPersons.First().TotalDiabetes;

        int pageIndex = gridSettings.pageIndex;
        int pageSize = gridSettings.pageSize;
        int totalRecords = extraPersons.Count;
        int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);

        int startRow = (pageIndex - 1) * pageSize;
        int endRow = startRow + pageSize;

        var jsonData = new
        {
            total = totalPages,
            page = pageIndex,
            records = totalRecords,
            userdata = new { TotalDiabetesCount = "totalDiabetes" },
            rows =
            (
              extraPersons.Select(e => new
              {
                  Id = e.ExtraPersonId,
                  Name = e.FirstName + " " + e.LastName,
                  Diabetes = e.Diabetes ,
                  BP = e.BP,
                  Arrived = e.Arrived,
                  TreatmentApplied = e.TreatmentApplied,                       

              })
            ).ToArray()
        };
        return Json(jsonData);

我的 jqGrid 的 JavaScript 代码如下,

     jQuery("#allextraperson").jqGrid({
            url: '/ExtraPerson/GetAllExtraPersons/',
            datatype: 'json',
            mtype: 'POST',

            colNames: gridInfoJSON.GridModel.ColumnDisplayArray,
            colModel: [
            { name: 'Id', index: 'Id', hidden: true },
            { name: 'Name', index: 'Name', width: 200, formatter: generateNameLink },
            { name: 'Diabetes', index: 'Diabetes', width: 55, align: 'center', formatter: generateDiabetesLabel },
            { name: 'BP', index: 'BP', width: 50, align: 'center', formatter: generateBPLabel },
            { name: 'Arrived', index: 'Arrived', width: 70, align: 'center', formatter: generateArrivedLabel },               
            { name: 'Delete', index: 'Delete', width: 75, align: 'center', formatter: deleteformatter }

       ],
            jsonReader: {
                id: 'Id',
                repeatitems: false                   
            },
            height: "100%",
            width: 970,
            pager: '#allextraperson-pager',
            rowNum: 10,
            rowList: [10, 20, 30, 50],
            sortname: 'Id',
            sortorder: 'asc',
            viewrecords: true,                 
            loadComplete: function (data) {
                var table = this;
                setTimeout(function () {
                    updatePagerIcons(table);

                }, 0);

                var totaldiabetes = jQuery("#allextraperson").getGridParam('totalDiabetes');                                     
                alert("Total diabetes:" + totaldiabetes);
            }               
        });
    }

我的查看代码显示 TotalDiabetes 如下

<span class="user_info muted" style="position: inherit;">Total Diabetes
        :&nbsp <strong class="orange"> totalDiabetes </strong></span>

警报显示总糖尿病:null 我不知道我做错了什么,请任何人建议我一个解决方案。

4

1 回答 1

2

我自己发现我做错了什么,在控制器代码中,我已经替换了这一行

userdata = new { TotalDiabetesCount = "totalDiabetes" },

用这条线

userdata = new { totalDiabetes = TotalDiabetesCount }

在我的 javascript 代码中,我已经替换了该行

var totaldiabetes = jQuery("#allextraperson").getGridParam('totalDiabetes');

有了这些线条,

var userData = jQuery("#allextraperson").getGridParam('userData');
                if (userData.totalDiabetes) {
                    $('#total-diabetes').text(userData.totalDiabetes);
                }

在我的视图中,我id = "total-diabetes"在 html 中添加了一个 to strong 标签。因此我得到了输出。

于 2013-08-31T13:42:56.220 回答