我正在开发一个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();
}
最后这是我在我的MVC项目中加载的JqGrid:
<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 moethod输出的内容:
{{"$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
发送到我的控制器的urlhttp://localhost:41389/api/well?_search=false&nd=1384556871623&rows=20&page=1&sidx=&sord=asc
答案 0 :(得分:0)
您的问题出在您为行返回的数据结构中。当您返回单元格时,您没有传递网格可以为列解析的任何“关键”信息。因此,只需返回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命名空间,则需要包含
答案 1 :(得分:0)
好吧,我想通了,我觉得很蠢。
显然在Chrome中进行调试效果不佳。将部署更改为IE,现在工作正常-_-
答案 2 :(得分:0)
我正面临类似的问题。在IE中调试后,我发现了一个问题。按钮控件没有用引号括起来$(“#btnContactList”)。click(function(){}
即使纠正后,我仍然面临着一个问题。数据不会加载到jqGrid中。我想知道这是否是一个可靠的控制来看待这些问题。 这是我发布的问题。看看你们是否可以提供帮助。
jqGrid not rendering data - Headers visible
我已经解决了这个问题。有关详情,请参阅我的帖子。缺少jsonReader属性并导致问题。问题解决了。