jgGrid没有使用asp.Net MVC 3.0显示数据

时间:2011-05-13 17:16:13

标签: asp.net-mvc-3 jqgrid

我在jgGrid 4.0中显示从我的视图返回的json数据时出现问题 在头部我有

<script src="/Scripts/jquery-1.5.2.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>

    <script src="/Scripts/jquery.lazyload.min.js" type="text/javascript"></script>
<script src="/Scripts/global.js" type="text/javascript"></script>

  <script src="/Scripts/jquery-ui-1.8.11.min.js" type="text/javascript"></script>

  

身体

    $(document).ready(function(){

    jQuery("#grid").jqGrid({
        url: '@Url.Action("getusers", "dashboard",new {area="Security"})',
        datatype: "json",

        mtype: "GET",
        colNames: ['Id', 'UserName'],

        colModel: [
        { name: 'Id', index: 'Id',width: 200, align: 'left'},
        { name: 'UserName', index: 'UserName', width: 200, align: 'right' }

        ],
        rowList: null,        
        pgbuttons: false,     
        pgtext: null,        
        viewrecords: false,

      page:false,
        caption: "Users"
    });
    });

这里返回一个json的Action代码

public JsonResult GetUsers()
        {
            var repo = ObjectFactory.GetInstance<IRepository<User>>();
            var result = (from x in repo.Query(x => x.ApplicationName == "DBM") select new {Id=x.Id, UserName=x.UserName}).ToArray();
            return this.Json(result, JsonRequestBehavior.AllowGet);
        }
    }

我在firefox和IE 9中测试过网格渲染为空,firebug没有错误,数据看起来还不错。 任何提示都将不胜感激。

1 个答案:

答案 0 :(得分:3)

jqGrid需要特定的json格式:

试试这个

 var jsonData = new
     {
         total = (rowcount + paging.Size - 1) / paging.Size
         page = paging.Page,
         records = rowcount,
         rows = (
                 from x in repo.Query(x => x.ApplicationName == "DBM")
                 select new 
                 {
                    id=x.Id,
                    cell = new string[]
                    {
                      // the order of the columns here must match 
                      x.Id, 
                      x.UserName
                    }
                })
     };

     return Json(jsonData, JsonRequestBehavior.AllowGet);

请参阅using jquery grid with asp.net mvc