0

我的 .cshtml 如下:

<script type="text/javascript">
        $(function () {
            $("#list").jqGrid({
                url: '/LogInManagement/DisplayItems/',//this is action in controller which returns the json as result.
                datatype: 'json',
                mtype: 'GET',
                colNames: ['ID', 'Product Name', 'Description', 'IsInStock', 'Attachment'],//column name
                colModel: [
          { name: 'ID', index: 'Id', width: 40, align: 'left' },
          { name: 'ProductName', index: 'ProductName', width: 240, align: 'left' },
          { name: 'Description', index: 'Description', width: 200, align: 'left' },
          { name: 'IsInStock', index: 'IsInStock', width: 40, align: 'left' },
          { name: 'Attachment', index: 'Attachment', width: 240, align: 'left' }],
                pager: jQuery('#pager'),
                rowNum: 10,
                rowList: [5, 10, 20, 50],
                sortname: 'Id',
                sortorder: "desc",
                viewrecords: true,
                caption: 'Items'
            });
        }); 
    </script> 
@{
    ViewBag.Title = ViewBag.DisplayName;
}

<h2>Welome : @ViewBag.DisplayName</h2>
<table id="list" class="scroll" cellpadding="0" cellspacing="0"></table>
<div id="pager" class="scroll" style="text-align:center;"></div>

我正在调用方法“DisplayItems”,如下所示:

public ActionResult DisplayItems()
    {
        Item item = new Item();
        int pages = 0;
        var resultItems = Assignment.Models.Item.GetItems().ToArray();
        var jsonData = new
        {
            total = 1,
            page = pages,
            records = resultItems.Count(),
            rows = resultItems
        };
        return Json(jsonData, JsonRequestBehavior.AllowGet);
    }

它正在正确返回数据。但是cshtml中的网格显示“正在加载...”,但没有数据。数据采用以下格式:

在此处输入图像描述

4

1 回答 1

0

几个月前我遇到了同样的问题,我通过改变下面的 Action 方法解决了这个问题(保持 cshtml 文件原样),

    using System.Web.Script.Serialization;

    public string DisplayItems()
        {
            Item item = new Item();

        int pages = 0;
        var resultItems = Assignment.Models.Item.GetItems().ToArray();
        var jsonData = new
        {
            total = 1,
            page = pages,
            records = resultItems.Count(),
            rows = resultItems
        };
        JavaScriptSerializer js = new JavaScriptSerializer();
        string Serializeddata = js.Serialize(jsonData );
        return Serializeddata;
    }

注意返回类型string,我JavaScriptSerializer用来返回字符串。请让我知道这对你有没有用。干杯。

于 2013-09-16T06:15:31.317 回答