2013-08-21 85 views
1

Mine是一个MVC项目。在我看来,我正在做一个AJAX调用来获取JSON数据。从我的控制器时我deseralize使用javascriptDeserializer我的课,我得到以下JQGrid在通过AJAX获取JSON数据时不显示JSON数据

{ 
    "page":1, 
    "total":2, 
    "records":2, 
    "rows":[ 
     {"id":1,"cell":["ToolVersion","1","ESP","1.2","2/2/2013"]}, 
     {"id":2,"cell":["ToolVersion","2","OPT","1.3","3/3/2013"]} 
    ] 
} 

的操作方法如下

var lst = obj.GetDetails(); // this returns the instance of MyCustomJQGrid class. 
return Json(new 
{ 
    total = 2, 
    page = 1, 
    records = 2, 
    rows = lst.rows.ToArray() 
}, JsonRequestBehavior.AllowGet); 

public class MyCustoMJQGrid 
{ 
    public class Row 
    { 
     public int id { get; set; } 
     public List<string> cell { get; set; } 
     public Row() 
     { 
      cell = new List<string>(); 
     } 
    } 

    public int page { get; set; } 
    public int total { get; set; } 
    public int records { get; set; } 
    public List<Row> rows { get; set; } 
    public JQGrid() 
    { 
     rows = new List<Row>(); 
    } 
} 
鉴于

,我使用下面的代码

$('#list2').jqGrid({ 
     url: '@(Url.Action("GetToolVersionDetails", "Admin"))', 
     datatype: 'json', 
     colNames: ['TableName','RowId', 'ColA', 'ColB', 'ColC'], 
     mtype: 'GET', 
     colModel: [ 
        { name: 'TableName', index: 'TableName', width: 150 }, 
        { name: 'RowId', index: 'RowId', width: 150 }, 
        { name: 'ColA', index: 'ColA', width: 250 }, 
        { name: 'ColB', index: 'ColB', width: 250 }, 
        { name: 'ColC', index: 'ColC', width: 250 } 
     ], 
     jsonReader: { 
      root: 'rows', 
      total: 'total', 
      page: 'page', 
      records: 'records', 
      cell: 'cell', 
      id: 'id', 
      repeatitems: false 
     }, 
     rowNum: 10, 
     rowList: [5, 10, 20, 30], 
     pager: $('#gridpager'), 
     sortname: 'RowId', 
     viewrecords: true, 
     sortorder: 'asc', 
     caption: 'Tool Version', 
     shrinkToFit: true, 
     width: $('#gridContainer').width(), 
     height: 200, 
     hidegrid: false, 
    }); 

的jqGrid越来越显示两个空行。没有数据显示。它只是显示一个有两行的空白表。 JSON数据没有得到显示。

请帮助。

谢谢。

回答

1

我建议你安装并使用MvcJqGrid nuget软件包,以使用mvc助手提供全功能的流畅网格解决方案。

它表示一个存储库类来选择,筛选和排序行。它也代表了一个GridSettings类型,您可以在控制器中简单地使用它来将数据行放入json响应并将其发送到视图。

例子是here

存储库类的代码是here。您还可以找到所有其他所需的代码

+0

问题是与json格式。我们需要将列名称与值一起使用。否则我们应该设置Repeatvalues = true。 – user1447718