我有一个JQGrid treegrid,需要从ASP.NET MVC项目移到WebForms项目。将jqGrid从ASP.NET MVC移动到WebForms项目时需要进行更改
我已经决定在项目中使用web服务来返回网格的数据,因为我没有太多编写自定义处理程序的经验。
这里是我现有电网:
$("#TreeGrid").jqGrid({
url: '@Url.Action("GetSPTreeJSON", "Index")',
treeGrid: true,
datatype: "json",
mtype: "POST",
treeGridModel: 'adjacency',
postData: { currentUserGroup: currentUserGroup },
colNames: ["id", "partners"],
colModel: [
{ name: 'id', index: 'id', hidden: true, key: true },
{ name: 'partners', index: 'name', width: 500, sortable: false, classes: 'pointer' }
],
ExpandColumn: 'partners',
gridview: true
});
和现有的控制器动作:
public JsonResult GetSPTreeJSON(string nodeid, string n_level, Guid currentUserGroup)
{
List<TreeNode> list = clientService.GetTreeNodeList(nodeid, n_level, currentUserGroup);
var jsonData = new
{
page = 1,
total = 1,
records = 1,
rows = (
from TreeNode u in list
select new
{
cell = new object[] { u.Id.ToString(), u.name, u.level, u.ParentId, u.isLeaf, false }
}).ToList()
};
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
样品JSON(为根节点):
{
"page":1,
"total":1,
"records":1,
"rows":[
{
"cell":[
"5a1a9742-300f-11d3-941f-036008032006",
"root node",
0,
null,
false,
false
]
}
]
}
我的问题是:究竟什么需要使用json数据和webservices在asp.net webforms项目中使用它?
我有半至今工作:
添加或更改以下的jqGrid选项:
url: 'JQGridData.asmx/GetSPTreeJSON', //now points to the webservice
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' }, //looks like it wont hit the webservice if this isn't included
serializeGridData: function (postData) { //looks like this is required because the webservice call will fail if not passed the correct parameters
if (postData.nodeid === undefined) postData.nodeid = null;
if (postData.n_level === undefined) postData.n_level = null;
return JSON.stringify(postData);
},
WebService的骨架,需要返回JSON:
编辑:
按照Olegs的建议,我编辑了我的webservice方法WS:
public class jqGridRecord
{
public Guid Id { get; set; }
public string Name { get; set; }
public int level { get; set; }
public Guid? parent { get; set; }
public bool isLeaf { get; set; }
public bool expanded { get; set; }
}
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class JQGridData : System.Web.Services.WebService
{
private readonly ClientService clientService = new ClientService();
[WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public JqGridData GetSPTreeJSON(string nodeid, string n_level, Guid currentUser)
{
List<TreeNode> list = clientService.GetTreeNodeList(nodeid, n_level, currentUser);
List<jqGridRecord> data = new List<jqGridRecord>();
foreach (var i in list)
{
jqGridRecord r = new jqGridRecord
{
Id = i.Id,
Name = i.name,
level = i.level,
parent = i.ParentId,
isLeaf = i.isLeaf,
expanded = false
};
data.Add(r);
}
int recordsCount = data.Count;
return new JqGridData()
{
total = 1,
page = 1,
records = recordsCount,
rows = data
};
}
}
public class JqGridData
{
public int total { get; set; }
public int page { get; set; }
public int records { get; set; }
public List<jqGridRecord> rows { get; set; }
}
,并增加了以下的jqGrid的选项:
colModel: [
{ name: 'Id', hidden: true, key: true },
{ name: 'Name', width: 500, sortable: false, classes: 'pointer' }
],
jsonReader: { repeatitems: false, root: "d.rows", page: "d.page", total: "d.total", records: "d.records" },
以下JSON正在返回的根,我可以看到根树中的节点,但不能点击+图标来展开它......似乎jsonReader仍然存在某种问题?
{
"d":{
"__type":"SSDS.iValue.JqGridData",
"total":1,
"page":1,
"records":1,
"rows":[
{
"Id":"5a1a9742-3e0f-11d3-941f-006008032006",
"Name":"acsis Limited",
"level":0,
"parent":null,
"isLeaf":false,
"expanded":false
}
]
}
}
感谢Oleg,我已经应用并理解了您提出的更改......但仍然存在无法展开根节点的问题。请看我的编辑! – woggles 2013-04-30 07:32:25
啊想出来了......对于ExpandColumn不会错的!感谢您的帮助:) – woggles 2013-04-30 08:07:51
@woggles:您现在在Web服务中使用的代码应该和ASP.NET应用程序所做的一样。 TreeGrid将树参数发送到服务器'nodeid','parentid''n_level'(请参阅[文档](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:adjacency_model#what_we_post))。服务器应该返回节点的子节点,这些节点的子节点将被消耗掉。你应该改变你的服务器代码 – Oleg 2013-04-30 08:07:54