gpt4 book ai didi

Jquery 网格与 ASP.NET

转载 作者:行者123 更新时间:2023-12-01 01:09:44 25 4
gpt4 key购买 nike

我正在尝试将 Jquery 网格与 asp.net 一起使用,但它不起作用,它显示网格内容为空,我不确定我的代码有什么问题!这是我的 HTML 代码:

<script type="text/javascript">
$(function () {
$("#list").jqGrid({
url: '/WebServices/Admin/WebMethods.ashx',
datatype: 'json',
mtype: 'POST',
colNames: ['ID', 'Name', 'Description'],
colModel: [
{ name: 'ID', index: 'ID', width: 55 },
{ name: 'NAME', index: 'NAME', width: 90 },
{ name: 'DESCRIPTION', index: 'DESCRIPTION', width: 80 }

],
jsonReader: {
repeatitems:false
},
pager: $('#pager'),
rowNum: 10,
rowList: [10, 20, 30],
sortname: 'ID',
sortorder: 'desc',
viewrecords: true,

caption: 'Lockups'
}).navGrid('#pager');
});
</script>

随后:

<form runat="server">
<div style="width:700px">
<table id="list" width="100%">
<tr>
<td />
</tr>
</table>
<div id="pager">
</div>
</div>
</form>

我的 C# 代码,我正在将对象列表转换为 JSON:

 public void ProcessRequest(HttpContext context)
{

context.Response.ContentType = "application/json";
context.Response.Write(GetAllLookups());
}

public string GetAllLookups()
{
var lst = from lockup in LOCKUP_ITEMS.GetLockups()
select new {
ID = lockup.ID,
NAME = lockup.NAME,
DESCRIPTION = lockup.DESCRIPTION
};

return Newtonsoft.Json.JsonConvert.SerializeObject(
lst,
new JavaScriptDateTimeConverter());
}

最佳答案

jqGrid 需要特定格式的 json 数据:

{ 
"total": "xxx",
"page": "yyy",
"records": "zzz",
"rows" : [
{"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
{"id" :"2", "cell":["cell21", "cell22", "cell23"]},
...
]
}

因此将您的 GetAlLookups 方法更改为以下内容:

public string GetAllLookups()
{
var list = LOCKUP_ITEMS.GetLockups();
var numOfItems = list.Count();

var result = new {
total = numOfItems ,
page = 1,
records = numOfItems ,
rows = (from lockup in list
select new {
ID = lockup.ID,
NAME = lockup.NAME,
DESCRIPTION = lockup.DESCRIPTION
}).ToArray()
};
return Newtonsoft.Json.JsonConvert.SerializeObject(
result,
new JavaScriptDateTimeConverter());
}

关于Jquery 网格与 ASP.NET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9538035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com