jqGrid数据不会在json的MVC4中显示数据

时间:2013-03-04 04:52:34

标签: jquery jqgrid

我是jqGrid的新手。我试图创建一个简单的例子,其中controller返回一个包含模型数组数据的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>

1 个答案:

答案 0 :(得分:0)

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

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

                },

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

而不是使用网格选项中的url来调用服务器方法

url:'your_url_which_invoke_server_method',