【发布时间】: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