jqGrid userData在View中显示一个值(asp.net mvc3)

时间:2013-08-31 08:15:59

标签: javascript jquery asp.net-mvc-3 jqgrid

我是jqGrid的新手。我的任务现在是在视图中显示TotalDiabetes(asp.net MVC3),它有jqGrid,所以我选择了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>

警报显示总糖尿病:无效 我不知道我做错了什么,请有人建议我解决。

1 个答案:

答案 0 :(得分:2)

我自己发现了我做错了什么,在Controller代码中,我已经替换了行

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);
                }

在我的视图中,我在html中为强标记添加了id = "total-diabetes"。因此我得到了输出。