JqG​​rid不显示数据MVC

时间:2016-06-01 14:48:49

标签: jquery asp.net-mvc asp.net-mvc-4 jqgrid

请检查我正在使用的jqgrid,它只显示空白网格,我的json是根据网格的预期格式。我正在使用jqGrid 4.4.4

<script type="text/javascript">
        $(function () {
            $("#myGrid").jqGrid({
                url: '/Home/GetData/',
                datatype: "json",
                contentType: "application/json; charset-utf-8",
                mtype: 'GET',
                colNames: ['CP', 'Val1', 'Val2', 'Val3'],
                colModel: [
                                { name: 'CP', index: 'CP', width: 150 },
                                { name: 'Val1', index: 'Val1', width: 150 },
                                { name: 'Val2', index: 'Val2', width: 150 },
                                { name: 'Val3', index: 'Val3', width: 150 }
                ],
                rowNum: 5,
                rowList: [5, 10, 15],
                pager: '#pager',
                sortname: 'CP',
                viewrecords: true,

                sortorder: "asc",

                viewrecords: true,

                caption: "JSON Example"
            });
            $("#myGrid").jqGrid('navGrid', '#pager', { edit: true, add: true, del: true });

        });

    </script>

我的控制器方法看起来像我按照jqGrid文档格式化数据。

 [HttpGet]
        public JsonResult GetData()
        {
            List<Rate> myList = CallProcedure<Rate>("getVal").ToList();


            var jsonData = new
            {
                total = myList .Count,
                page = 1,
                records = 10,
                rows = (
                  from d in myList 
                  select new
                  {
                      id = d.CP,
                      cell = new string[] {
                      d.CP.ToString(),
                      d.Val1.ToString(),
                      d.Val2.ToString(),
                      d.Val3.ToString()
                      }
                  }).ToArray()
            };



            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

尝试在网格中设置jsonReader但仍然没有成功,请你指出我在哪里错了?提前致谢

Json看起来如下

   {
    "total": 1,
    "page": 1,
    "records": 25,
    "rows": [{
        "id": "AUSD",
        "cell": ["AUSD    ", "0.000000", "0.72315000", "0.000000"]
    }, {
        "id": "PPFF",
        "cell": ["PPFF    ", "0.000000", "1.10288000", "0.000000"]
    }, {
        "id": "XTYU",
        "cell": ["XTYU    ", "0.000000", "1.41479000", "0.000000"]
    }, {
        "id": "NUSD",
        "cell": ["NUSD    ", "-0.000020", "0.67097000", "-0.000020"]
    }, {
        "id": "USED",
        "cell": ["USED    ", "0.000000", "3.67278000", "0.000000"]
    }, {
        "id": "UAD",
        "cell": ["UAD    ", "0.000120", "1.37037000", "0.000020"]
    }]
}

新json看起来像,

{
    "total": 1,
    "page": 1,
    "records": 25,
    "rows": [
        ["Val1    ", "0.000000", "0.72315000", "0.000000"],
        ["Val12    ", "0.000000", "1.10288000", "0.000000"],
        ["Val13    ", "0.000000", "1.41479000", "0.000000"],
        ["Val14    ", "-0.000020", "0.67097000", "-0.000020"],
        ["Val15    ", "0.000000", "3.67278000", "0.000000"],
        ["Val16    ", "0.000120", "1.37037000", "0.000020"],
        ["Val17    ", "0.000000", "0.99843000", "0.000000"],
        ["Val18    ", "0.000000", "24.53100000", "0.000000"],
        ["Val19    ", "0.000000", "6.76500000", "0.000000"],
        ["Val23    ", "0.000000", "7.77250000", "0.000000"]

    ]
}

网格仍为空白:(

更新操作方法以不同格式返回json,

{
    "total": 25,
    "page": 1,
    "records": 10,
    "rows": [{
        "CP": "ADRR",
        "Val1": "0.000000",
        "Val2": "0.72315000",
        "Val3": "0.000000"
    }, {
        "CP": "TRRT",
        "Val1": "0.000000",
        "Val2": "1.10288000",
        "Val3": "0.000000"
    }, {
        "CP": "TRER",
        "Val1": "0.000000",
        "Val2": "1.41479000",
        "Val3": "0.000000"
    }]
}

json的区别在于它包含“for”CP“,”Val1“名称。并且网格仍为空白

这里有什么问题?当页面加载时,断点就会出现在这里,它会按预期调整json,

    public JsonResult GetData()
            {
                List<Live> dd = CallProc<Live>("getLiveData").ToList();


                var jsonData = new
                {
                    total = 1,
                    page = 1,
                    records = dd.Count,
                    rows = (
                      from d in dd
                      select new string[]
                      {
                          CP = d.CP.ToString(),
CP1 = d.CP1.ToString(),
                           CP2 = d.CP2.ToString(),
Cp3 =                           d.Cp3.ToString()
                      }).ToArray()

                };


                string json = new JavaScriptSerializer().Serialize(jsonData); //just to check what json I am getting

                return Json(jsonData, JsonRequestBehavior.AllowGet);
            }

3 个答案:

答案 0 :(得分:2)

我在评论中建议的是:

  {
"total": 1,
"page": 1,
"records": 25,
"rows": [["AUSD    ", "0.000000", "0.72315000", "0.000000"], 
         ["2", "PPFF    ", "0.000000", "1.10288000", "0.000000"],
         ["XTYU    ", "0.000000", "1.41479000", "0.000000"]

] }

我总是会遇到关于JSON结果的多个网格问题并且它已经解决了。

答案 1 :(得分:1)

JqGrid不接受行中的任何json,它需要在名称值对中格式化的json,它与列模型中的name字段匹配。

Here JsFiddle Sample

{
    "total": 1,
    "page": 1,
    "records": 25,
    "rows": [
        {CP: "Val1    ", Val1: "0.000000", Val2:"0.72315000", Val3: "0.000000"},
        {CP: "Val12    ",Val1: "0.000000", Val2: "1.10288000",Val3: "0.000000"},
        .....

    ]
}

要获得该json,您需要更新您的操作方法,如下所示

 public JsonResult GetData()
        {
            List<Rate> myList = CallProcedure<Rate>("getVal").ToList();


            var jsonData = new
            {
                total = myList .Count,
                page = 1,
                records = 10,
                rows = (
                  from d in myList 
                  select new
                  {
                     id = d.CP,
                     CP = d.CP.ToString(),
                     Val1= d.Val1.ToString(),
                     Val2 = d.Val2.ToString(),
                     Val3=  d.Val3.ToString()
                  }).ToArray()
            };



            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

答案 2 :(得分:0)

您是否在调试器中检查了服务器的结果? 控制台中是否有任何问题?

如果没有,可能会尝试删除jsonData块中的“ToArray()”。

            var jsonData = new
        {
            total = myList .Count,
            page = 1,
            records = 10,
            rows = (
              from d in myList 
              select new
              {
                  id = d.CP,
                  cell = new string[] {
                  d.CP.ToString(),
                  d.Val1.ToString(),
                  d.Val2.ToString(),
                  d.Val3.ToString()
                  }
              })
        };