JQGrid没有显示数据
我对MVC和Jquery相当陌生。最近几天我试图用Jqgrid http://www.trirand.com/blog/在我的数据库中显示数据。我使用EF代码首先创建我的唯一一类“作者”JQGrid没有显示数据
public class Author
{
public int AuthorID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string FullName
{
get
{
return FirstName+ " "+LastName ;
}
}
}
,这是我的“AuthorController”的创建JSON数据:
public ActionResult LinqGridData(string sidx, string sord, int page, int rows)
{
var jsonData = new
{
total = 5,
page = 1,
records = db.Authors.Count(),
rows = db.Authors.Select(a => new
{
id = a.AuthorID,
cell = new { a.AuthorID, a.FirstName, a.LastName }
}
)
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
我也尝试了不同的方法,让我的JSON数据:
public ActionResult LinqGridData (string sidx, string sord, int page, int rows)
{
var jsonData = new {
total = 5,
page=1,
records = db.Authors.Count(),
rows = (from a in db.Authors
select new
{
id = a.AuthorID,
cell = new { a.AuthorID, a.FirstName, a.LastName }
}
)
};
return Json(jsonData,JsonRequestBehavior.AllowGet);
}
,这里是我的JavaScript,我在我的视图中使用:
$(function() {
$("#list").jqGrid({
url: '/Author/LinqGridData',
datatype:'json',
mtype: 'GET',
colNames:['Author ID', 'First Name', 'Last Name'],
colModel:[
{name:'AuthorID',index:'AuthorID',width:55 },
{name:'FirstName',index:'FirstName',width:90 },
{name:'LastName',index:'LastName',width:80,align:'right' }
],
pager:'#pager',
rowNum: 10,
rowList:[5, 10, 20, 30],
sortname:'AuthorID',
sortorder:'desc',
viewrecords:true,
gridview:true,
caption:'Author List'
});
});
jQuery("#datagrid").jqGrid('navGrid', '#navGrid', { edit: true, add: true, del: true });
我可以用虚拟数据显示网格。这种操作方法:
public ActionResult GridData(string sidx, string sord, int page, int rows)
{
var jsonData = new
{
total = 1, // we'll implement later
page = 1,
records = 3, // implement later
rows = new[]
{
new {id = 1, cell = new[] {"1", "-7", "Is this a good question?"}},
new {id = 2, cell = new[] {"2", "15", "Is that a good question?"}},
new {id = 3, cell = new[] {"3", "23", "Why is the sky in the sky?"}}
}
};
return Json(jsonData,JsonRequestBehavior.AllowGet);
}
问题是,每当我想从我的数据库显示的数据,我只可以显示网格本身不是数据。 我试图在发送到视图之前将json数据转换为toList()或toArary(),结果相同。我希望我明确自己。
任何帮助,将不胜感激。
我终于设法从我的数据库显示数据。问题出在我的查询中。我用这个方法对我的操作方法:
public JsonResult LinqGridData (string sidx, string sord, int page, object rows)
{
var authors = (from a in db.Authors select a).ToList();
var jsonData = new {
total = 5,
page=1,
records = db.Authors.Count(),
rows = authors.Select(a => new
{
id = a.AuthorID,
cell = new[] { a.AuthorID.ToString(), a.FirstName, a.LastName }
}
)
};
return Json(jsonData,JsonRequestBehavior.AllowGet);
}
,我用这个在我的javascript:
$(function() {
$("#list").jqGrid({
url: "/Author/LinqGridData",
datatype: "json",
jsonReader: {
root: "rows",
page: "page",
total: "total",
records: "records",
cell: "cell",
id:"id"
},
mtype: "GET",
colNames: ["Author ID", "First Name", "Last Name"],
colModel: [
{ name: "AuthorID", key: true, width: 55 },
{ name: "FirstName", width: 80 },
{ name: "LastName", width: 100, align: "right" }
],
pager: "#pager",
rowNum: 10,
rowList: [5, 10, 20],
sortname: "AuthorID",
sortorder: "desc",
viewrecords: true,
gridview: true,
width: 610,
height: 250,
caption: "Author List"
});
}); (“#list”)。jqGrid(“navGrid”,“#pager”,{edit:true,add:true,del:true});
是的,你可以省略'cell'属性并减少你的json数据。你也可以把'id':0这通常意味着把第一个项目作为id。我也使用'键'属性,但它是多余的。您也可以将您的查询结果作为Array传递。如果任何人都可以告诉我是否有任何使用列表比其他人的好处将非常感激。
感谢您的帮助 好运
你有没有尝试jsonReader与“repeatitems:false”? 例如:
$("#list").jqGrid({
url: '/Author/LinqGridData',
datatype:'json',
jsonReader: {
repeatitems: false
},
.....
UPDATE: 如果你从你的LinqGridData方法返回的响应主体,它看起来像这样:
{"total":5,"page":1,"records":1,"rows":
[
{"id":"1","cell":{"AuthorID":"1","FirstName":"Mike","LastName":"Lee"}}, .....
]
}
但我想应该是这样的:
{"total":5,"page":1,"records":1,"rows":
[
{"id":"1","cell":{"1", "Mike", "Lee"}}, .....
]
}
其实这是你的“伪数据”版本的响应体。
我会在下面发表我的工作示例。在这个例子中,我没有使用'cell'属性。
在服务器端:
var myQuery = from t in db.Customers
select t;
var jsonData = new
{
total = totalPages,
page = pageNum,
records = totalRecords,
rows = myQuery.ToList()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
在客户端:
{
url: '@Url.Action("GetList")',
datatype: 'json',
jsonReader: {
repeatitems: false
},
mtype: 'GET',
colModel: [
{
name: 'CustomerID', label: 'ID', hidden: false, width: 40, sortable: true
},
{
name: 'CompanyName', label: 'Company', width: 100, align: 'center'
},
]
}
希望这有助于。
是的,当我试图甚至是jsonReader这个虚拟数据没有出现。我jsonReader是:jsonReader:{ 根: “行”, 页: “页”, 总: “总”, 记载: “记录”, repeatitems:假的, 细胞: “细胞”, ID: “id”, userdata:“userdata”, } – Pamador 2013-03-03 10:35:21
我已更新我的答案并添加了一些示例代码。 – 2013-03-03 11:09:42
我是这个社区中最少的,但我认为你的代码是错误的。你没有指定你从查询中选择的内容?你的Json数据的形状取决于你的JsonReader。如果你没有为'cell'定义任何东西,那么你可以没有明确地说'cell:...'。这里有一件有趣的事情,只要我将JsonReader添加到我的JS中,即使那些虚拟数据没有显示出来。我的JsonReader是:jsonReader {root:“rows”,page:“page”,total:“total”,records:“records”,repeatitems:false,cell:“cell”,id:“id”,}。我不知道我的问题在哪里?无论如何,帮助你。 – Pamador 2013-03-03 13:12:28
尝试使用工具,如提琴手检查正在返回什么JSON的页面,试图调试一个错误这样的时候。 – 2013-03-03 11:32:55
@Gabriel,我正在使用Fiddler。我可以在提琴手中看到我的Json数据,但不能在Jqgrid中看到。网格是空的。 – Pamador 2013-03-03 13:03:57
在您的描述中,代码返回到jqgrid的数据将是一个很有用的数据 – 2013-03-03 13:48:01