【问题标题】:jqGrid data not loadingjqGrid数据未加载
【发布时间】:2013-11-15 21:16:54
【问题描述】:

我正在开发一个 MVC 项目,该项目还包括一个 WEB API 项目。基本上我从我的 MVC 项目调用 API 项目以查询将出现在 jqGrid 中的数据。但是,我无法在网格中加载任何数据,它只是说“正在加载”,然后什么也没有发生。这是我如何设置所有内容的方式:

我在 Web API 端的控制器:

static readonly IWellRepository repository = new WellRepository();

    WellsMigrationEntities db = new WellsMigrationEntities();

    // GET api/values
   public dynamic Get(string sidx, string sord, int page, int rows)
    {
      var wells = repository.GetAll() as IEnumerable<vwWell>;
      var pageIndex = Convert.ToInt32(page) - 1;
      var pageSize = rows;
      var totalRecords = wells.Count();
      var totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
      wells = wells.Skip(pageIndex * pageSize).Take(pageSize);
      return new
      {
          total = totalPages,
          page = page,
          records = totalRecords,
          rows = (
              from well in wells
              select new
              {
                  //i = well.APINumber,
                  cell = new string[] {
                     well.APINumber,
                     well.CountyName,
                     well.LeaseName,
                     well.WellNumber 
                  }
              }).ToArray()
      };
    }

在 Controller 中调用的我的存储库方法:

WellsMigrationEntities db = new WellsMigrationEntities();

    public IEnumerable<vwWell> GetAll()
    {
        return db.vwWells.Where(o => o.CountyName == "Butte").ToList();
    }

最后这里是我的 JqGrid,它被加载到我的 MVC 项目中:

<script>
var API_URL = "http://localhost:41389/api/well";
jQuery("#gridMain").jqGrid({
    url: API_URL,
    datatype: 'json',
    mtype: 'GET',
    pager: '#pagernav',
    sortable: true,
    height: 200,
    viewrecords: true,
    jsonReader: {
        repeatitems: false,
        page: function () { return 1; },
        root: function (obj) { return obj; },
        records: function (obj) { return obj.length; }
    },
    colNames: ['APINumber', 'CountyName', 'LeaseName', 'WellNumber'],
    colModel: [{ name: 'APINumber', index: 'APINumber', width: 40, editable: true, edittype: 'text' },
     { name: 'CountyName', index: 'CountyName', editable: true, edittype: 'text', width: 70 },
     { name: 'LeaseName', index: 'LeaseName', editable: true, edittype: 'text', width: 70 },
     { name: 'WellNumber', index: 'WellNumber', editable: true, edittype: 'text' }
    ],
    caption: "jqGrid",
    autowidth: true

});

无论我尝试什么,数据都不会加载!以下是我的控制器中的 Get 方法的输出:

{{"$id":"1","total":13,"page":1,"records":260,"rows":[{"$id":"2","cell":["00700001","Butte","Parrott Inv. Co.","9A-3"]},{"$id":"3","cell":["00700002","Butte","Wild Goose Gas Unit 1","9"]},{"$id":"4","cell":["00700003","Butte","Wild Goose Gas Unit 1","10"]},{"$id":"5","cell":["00700004","Butte","Wild Goose Gas Unit 1","6"]},{"$id":"6","cell":["00700005","Butte","Capital Co.","1"]},{"$id":"7","cell":["00700006","Butte","Estes","1"]},{"$id":"8","cell":["00700007","Butte","Capital Co.","E-1"]},{"$id":"9","cell":["00700008","Butte","Donohoe Fee","1"]},{"$id":"10","cell":["00700009","Butte","Donohoe Fee","2"]},{"$id":"11","cell":["00700010","Butte","T. W. Rodgers","1"]},{"$id":"12","cell":["00700011","Butte","Wahl Community","1"]},{"$id":"13","cell":["00700012","Butte","Towne","1"]},{"$id":"14","cell":["00700013","Butte","Wild Goose Gas Unit 1","1"]},{"$id":"15","cell":["00700014","Butte","Neaves-Parrott Inv.","2"]},{"$id":"16","cell":["00700015","Butte","Neaves-Parrott Inv.","7"]},{"$id":"17","cell":["00700016","Butte","Parrott Inv. Co.","1"]},{"$id":"18","cell":["00700018","Butte","Parrott Investment Co.","1"]},{"$id":"19","cell":["00700019","Butte","Urich Oil -Parrott","2"]},{"$id":"20","cell":["00700020","Butte","Parrott Investment Co.","2"]},{"$id":"21","cell":["00700021","Butte","Parrott Investment Co.","3"]}]}}

为什么没有为我加载数据?

更新:

顺便说一下,这是从 jqGrid 发送到我的控制器的 url

http://localhost:41389/api/well?_search=false&nd=1384556871623&rows=20&page=1&sidx=&sord=asc

【问题讨论】:

  • 我面临着类似的问题。在IE中调试后,我发现了一个问题。按钮控件没有用引号引起来 $("#btnContactList").click(function (){} 即使更正后,我仍然面临一个问题。数据不会加载到jqGrid中。我想知道它是否是一个可靠的控件正在查看此类问题。这是我发布的问题。看看你们是否可以提供帮助。stackoverflow.com/questions/30131283/… 我已经解决了这个问题。有关详细信息,请参阅我的帖子。这是 jsonReader 属性丢失并导致问题的原因。问题解决了。

标签: jquery asp.net-mvc json jqgrid asp.net-web-api


【解决方案1】:

您的问题在于您为行返回的数据结构。当您返回单元格时,您不会传递网格可以为列解析的任何“关键”信息。因此,只需返回 vwWell 对象的数组

 // GET api/values


public dynamic Get(string sidx, string sord, int page, int rows)
    {
      var wells = repository.GetAll() as IEnumerable<vwWell>;
      var pageIndex = Convert.ToInt32(page) - 1;
      var pageSize = rows;
      var totalRecords = wells.Count();
      var totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
      wells = wells.Skip(pageIndex * pageSize).Take(pageSize);
      var jsonData = new
      {
          total = totalPages,
          page = page,
          records = totalRecords,
          rows = wells.ToArray() //We need an array of objects for the rows
      };
      return jsonData;
    }

注意如果不包含 System.Web.Mvc 命名空间,则需要包含它

【讨论】:

  • 你甚至可以使用 Web API 项目返回一个 Json 对象吗?说 Json 在当前上下文中不存在,并且我添加了 MVC 命名空间。
  • 哦,我没有注意到您使用的是 Web API。我不知道。如果是,返回类型应该是 ActionResult
  • 是的,我使用 Web API 端来存储我所有的模型和数据库连接。在我的 MVC 方面,我所做的只是显示网格。我的 Json 格式有问题吗?
  • 是的,问题在于您的“单元”结构的填充方式。你需要在那里的键/值对。我会更新答案供您尝试。
  • 感谢您的更新,虽然没有用,但我明白您所说的没有任何关键数据。我现在看到的一个问题是格式中没有“单元格数据。它现在读取 {"$id":"1","total":13,"page":1,"records":260,"rows" :[{"$id":"2","APINumber":"00700001","CountyName":"Butte","LeaseName":"Parrott Inv. Co.”等........是否应该有一些东西表示一行的开始?顺便说一句,现在检索到的 JSON 数据是所有数据库数据,而不仅仅是我以前有指定的。谢谢你的帮助顺便说一句我需要它!
【解决方案2】:

好吧,我想通了,我觉得自己很愚蠢。

显然在 Chrome 中调试效果不佳。将部署更改为IE,现在可以正常使用-_-

【讨论】:

  • 我很高兴你弄清楚了网格。我不确定您的 API 如何获取数据,但听起来您可能遇到了跨域请求问题。 Chrome 默认不允许跨域请求,但 IE 允许。要在 Chrome 中禁用,请参阅此帖子。 stackoverflow.com/questions/4819060/…
猜你喜欢
  • 2016-03-15
  • 1970-01-01
  • 2012-10-27
  • 1970-01-01
  • 2014-04-22
  • 2011-08-11
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
相关资源
最近更新 更多