尽管控制器操作返回了正确的json数据,但jQuery Grid没有显示任何项目

时间:2011-02-15 00:20:34

标签: jquery asp.net-mvc asp.net-mvc-3 jqgrid

这是我的客户端代码:

$("#grid").jqGrid({
        url: "/Entry/EntryListingByBreed/",
        datatype: "json",
        mtype: "POST",
        postData: { showId: showId, breedId: breedId },
        colNames: ["Class", "NZKC Reg", "Registered name", "Date of birth", "Entered"],
        colModel: [
            { name: "ClassNumber", sortable: false, width: 50 },
            { name: "NZKCRegistration", sortable: false, width: 100 },
            { name: "RegisteredName", sortable: false, width: 200 },
            { name: "DateOfBirth",  sortable: false, width: 200 },
            { name: "Entered", sortable: false, width: 200 }
        ],
        pager: jQuery("#pager"),
        rowNum: 20,
        rowList: [10, 20, 40],
        altRows: true,
        altclass: "gridAltRow",
        viewrecords: true,
        caption: "Entries for " + breedName
    });

这是我的服务器端代码:

 [RequiresAuthentication]
        [HttpPost]
        public ActionResult EntryListingByBreed(int showId, int breedId, string sidx, string sord, int page, int rows)
        {
            using (var dataContext = new NZDogShowsEntities())
            {
                int pageIndex = page - 1;
                int pageSize = rows;
                var entries = from se in dataContext.ShowEntries
                              where se.ShowID == showId && se.Entry.BreedID == breedId
                              select se;
                int totalRecords = entries.Count();
                int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
                var showEntries = (from se in dataContext.ShowEntries.AsEntryListingAggregate().AsEnumerable()
                                   where se.ShowID == showId && se.Entry.BreedID == breedId
                                   select new
                                   {
                                       ID = se.ID,
                                       ClassNumber = se.Class.Number.ToString(),
                                       NZKCRegistration = se.Entry.NZKCRegNumber,
                                       RegisteredName = se.Entry.RegisteredName,
                                       DateOfBirth = se.Entry.DateOfBirth.GetValueOrDefault().ToLongDateString(),
                                       Entered = se.Entry.DateOfBirth.GetValueOrDefault().ToLongDateString()
                                   }).ToList();

                var jsonData = new
                {
                    total = totalPages,
                    page = page,
                    records = totalRecords,
                    rows = (
                    from entry in showEntries
                    select new
                    {
                        i = entry.ID,
                        cell = new string[]{
                            entry.ClassNumber,
                            entry.NZKCRegistration,
                            entry.RegisteredName,
                            entry.DateOfBirth,
                            entry.Entered
                        }
                    }).ToArray()
                };

                var viewData = Json(jsonData);
                return viewData;
            }
        }
    }

我的服务器端代码看起来比必要的更冗长,但在我进行故障排除时它很好。 viewData变量显示所有正确的数据,行数,页码等等。

网格已格式化,即。显示所有铬环绕等。但是,没有显示数据。即使是“Page x of y”也不正确。它显示“第0页”和<span>,其中记录的总数应为空白。我迷失在这里 - 可能会遗漏一些非常明显的东西,特别是因为我是这一切的新手。

任何帮助/指针(双关语)将不胜感激。

感谢。

编辑:好的,上面的服务器端代码已修复,现在生成JSON字符串。它看起来格式正确。我不会更新上面的代码,但会在下面提供JSON字符串。它解析正确,但仍然Firebug报告“无效标签”。网格也没有被填充!这是字符串 - 不能为我的生活发现问题:

{"total":2,"page":1,"records":25,"rows":[{"id":2,"cell":["2a","1234567","Test Dog","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":3,"cell":["4a","abcdef","Test Dog 2","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":4,"cell":["5a","abc123","Test Dog 3","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":5,"cell":["6a","asdf890","Test Dog X","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":6,"cell":["6","qwerty","Test Dog Y","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":7,"cell":["1","fw3asd","Test Dog 4","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":8,"cell":["10","asdfa","Test Dog 5","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":9,"cell":["11a","houh2","Test Dog 6","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":11,"cell":["8","xxxx","Test X","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":12,"cell":["8","zzzz","Test Z","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":13,"cell":["1","qqqq","Test Q","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":14,"cell":["8a","tttt","Test T","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":15,"cell":["8a","rrrr","Test R","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":16,"cell":["7","aaaa","Test A","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":17,"cell":["1","11223344","Test Entry","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":18,"cell":["1","66778899","asdfa","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":19,"cell":["1","13o24bo","qwlen;oq","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":20,"cell":["1","oubiou","asdfa","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":21,"cell":["1","o2no3","hohohop","Monday, 1 January 0001","Monday, 1 January 0001"]},{"id":22,"cell":["1","o48t2","Test 09808","Monday, 1 January 0001","Monday, 1 January 0001"]}]}

2 个答案:

答案 0 :(得分:1)

我知道当JSON数据格式不完美时,jquery会出现问题,我会在firebug中查看返回数据的样子,并确保所有属性都用双引号格式化。

另外,我能想到的另一件事就是它不在$ .ready(function {...})中;

答案 1 :(得分:0)

我发现了问题 - 其他人正在使用此代码将主jquery库升级到1.5。显然有些东西不向后兼容!我用1.4.4取代了它,一切都立即起作用了!