2013-03-04 99 views
0

我是一个新的jqGrid。我试图创建一个简单的例子,其中控制器返回一个包含模型数组数据的json对象。网页显示带有列名称的网格。数据本身不会出现。jqGrid数据不显示MVC4与json的数据

感谢您的帮助。自从三天以来我一直在尝试。

这里是我的控制器

List<ClientInvoice> clientInvoices = new List<ClientInvoice> 
      { 
       new ClientInvoice { Id=1, Name = "test", Note = "note", Amount = 300.00f, Tax = 10.00f, Total = 2111.00f}, 
       new ClientInvoice { Id=2, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 20.00f, Total = 320.00f}, 
       new ClientInvoice { Id=3, Name = "test3", Note = "note3", Amount = 300.00f, Tax = 30.00f, Total = 430.00f}, 
       new ClientInvoice { Id=4, Name = "test", Note = "note", Amount = 300.00f, Tax = 10.00f, Total = 210.00f}, 
       new ClientInvoice { Id=5, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 20.00f, Total = 320.00f}, 
       new ClientInvoice { Id=6, Name = "test3", Note = "note3", Amount = 300.00f, Tax = 30.00f, Total = 430.00f}, 
       new ClientInvoice { Id=7, Name = "test", Note = "note", Amount = 300.00f, Tax = 10.00f, Total = 210.00f}, 
       new ClientInvoice { Id=8, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 21.00f, Total = 320.00f}, 
       new ClientInvoice { Id=9, Name = "test3", Note = "note3", Amount = 400.00f, Tax = 30.00f, Total = 430.00f}, 
       new ClientInvoice { Id=11, Name = "test", Note = "note", Amount = 200.00f, Tax = 10.00f, Total = 210.00f}, 
       new ClientInvoice { Id=12, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 20.00f, Total = 320.00f}, 
       new ClientInvoice { Id=13, Name = "test3", Note = "note3", Amount = 400.00f, Tax = 30.00f, Total = 430.00f}, 
       new ClientInvoice { Id=14, Name = "test", Note = "note", Amount = 200.00f, Tax = 10.00f, Total = 210.00f}, 
       new ClientInvoice { Id=15, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 20.00f, Total = 320.00f}, 
       new ClientInvoice { Id=16, Name = "test3", Note = "note3", Amount = 400.00f, Tax = 30.00f, Total = 430.00f}, 
       new ClientInvoice { Id=17, Name = "test", Note = "note", Amount = 200.00f, Tax = 10.00f, Total = 210.00f}, 
       new ClientInvoice { Id=18, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 20.00f, Total = 320.00f}, 
       new ClientInvoice { Id=19, Name = "test3", Note = "note3", Amount = 400.00f, Tax = 30.00f, Total = 430.00f}, 
       new ClientInvoice { Id=21, Name = "test", Note = "note", Amount = 200.00f, Tax = 10.00f, Total = 210.00f}, 
       new ClientInvoice { Id=22, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 20.00f, Total = 320.00f}, 
       new ClientInvoice { Id=23, Name = "test3", Note = "note3", Amount = 400.00f, Tax = 30.00f, Total = 430.00f}, 
       new ClientInvoice { Id=24, Name = "test", Note = "note", Amount = 200.00f, Tax = 10.00f, Total = 210.00f}, 
       new ClientInvoice { Id=25, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 20.00f, Total = 320.00f}, 
       new ClientInvoice { Id=26, Name = "test3", Note = "note3", Amount = 400.00f, Tax = 30.00f, Total = 430.00f}, 
       new ClientInvoice { Id=27, Name = "test", Note = "note", Amount = 200.00f, Tax = 10.00f, Total = 210.00f}, 
       new ClientInvoice { Id=28, Name = "test2", Note = "note2", Amount = 300.00f, Tax = 20.00f, Total = 320.00f}, 
       new ClientInvoice { Id=29, Name = "test3", Note = "note3", Amount = 400.00f, Tax = 30.00f, Total = 430.00f}, 


      }; 

     System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); 

      //use Serialize method to convert patient list to json string 

      string clientInvoiceJson = serializer.Serialize(clientInvoices); 
      ViewBag.ClientInvoicesJson = clientInvoiceJson; 

和视图看起来像这样

<input type="hidden" id="clientInvoices" value="@ViewBag.ClientInvoicesJson" /> 
<table id="list47"></table> 
<div id="plist47"></div> 

<script type="text/javascript"> 

    var mydata1 = $("#clientInvoices").attr("value"); 
    var mydata = mydata1; 
    jQuery("#list47").jqGrid({ 
     data: mydata, 
     datatype: 'json', 
     height: 150, 
     rowNum: 10, 
     rowList: [10, 20, 30], 
     colNames: ['Inv No', 'Client', 'Amount', 'Tax', 'Total', 'Notes'], 
     colModel: [ 
      { name: 'Id', index: 'Id', width: 60 }, 
      { name: 'Name', index: 'Name', width: 100 }, 
      { name: 'Amount', index: 'Amount', width: 80 }, 
      { name: 'Tax', index: 'Tax', width: 80 }, 
      { name: 'Total', index: 'Total', width: 80 }, 
      { name: 'Note', index: 'Note', width: 150 } 
     ], 
     pager: "#plist47", 
     viewrecords: true, 
     caption: "Manipulating Array Data" 
    }); 

</script> 

回答

0

我认为你缺少jsonReader添加到您的网格选项

   jsonReader: { 
        repeatitems: false, 
        id: "Id" 

       }, 

为什么要在一个单独的函数中调用服务器方法,并在变量var mydata = mydata1中分配响应;

,而不是在网格选项,使用URL来调用服务器方法

url:'your_url_which_invoke_server_method', 
+0

jsonReader:{}没有帮助。我会尝试与你提到的网址。我需要弄清楚脚本内部jqGrid调用的样子。感谢您的答复。 – 2013-03-04 05:41:14