【问题标题】:jgGrid not showing data using asp.Net MVC 3.0jqGrid不使用asp.Net MVC 3.0显示数据
【发布时间】:2011-05-13 17:16:12
【问题描述】:

我在 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 中没有错误,数据看起来还可以。 任何提示将不胜感激。

【问题讨论】:

    标签: asp.net-mvc-3 jqgrid


    【解决方案1】:

    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

    【讨论】:

    • @Jason & @Sammy: 代码中有两个小错误:1) 而不是total = (rowcount / paging.Size) + 1 一个应该使用total = (rowcount + paging.Size - 1) / paging.Size 2) rows 的属性应该是id (不是Id) 和cell。因此,您应该(仅在一处)将Id=x.Id 替换为id=x.Id。此外,可以消除两次​​Id 的发帖。有关代码示例,请参见 the answer 的“更新”部分。
    • @Oleg,这对我有用。 rows = ( from x in repo.Query(x => x.ApplicationName == "DBM") select new { Id = x.Id, cell = new string[] { // 这里的列顺序必须匹配 x。 Id.ToString(), x.UserName } });我确定 Jason 的解决方案中是什么样的参数分页。
    • @Oleg。对 id 字段使用小写字母时为真。这就是当我剪切和粘贴我的剪切和粘贴代码时发生的情况。我认为我的分页计算是正确的,但我会仔细检查。谢谢。
    • @Jason Watts:如果 rowcount=0 total 必须是 0 (而不是像你的情况下的 1)。如果 paging.Size=1rowcount=1 total 必须是 1 (而不是 2 在你的情况下)。所以total = (rowcount / paging.Size) + 1 是错误的。现在尝试将total = (rowcount + paging.Size - 1) / paging.Size 设置为rowcountpaging.Size 的不同值,您会发现它总是正确的。
    • @Oleg。我懂了。当没有行时,我返回一个简化的 json 对象,所以我没有遇到这个问题。不过,我将在答案中编辑数学以供将来参考。谢谢!
    猜你喜欢
    • 2018-12-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    相关资源
    最近更新 更多