我的 jqGrid 没有填充在 Asp.net MVC 4 中提交我的表单...我想找出原因...我有一个返回此 Json 字符串的 ajax 方法,它是网络响应浏览器中的 Json 字符串...
"{\"total\":1,\"page\":1,\"records\":1,\"rows\":[{\"cell\":[\"\",\"342352857634\",\"test\",\"test\",\"5/8/2012 11:39:38 AM\",\"1\",\"\",\"1/1/0001 12:00:00 AM\",\"1/1/0001 12:00:00 AM\",\"1/1/0001 12:00:00 AM\"]}]}"
这是完成所有魔术的功能。
$('#SearchPatID').submit(function (event) {
//alert("What the motherFuck is going on here!");
debugger;
var theURL = this.action;
var type = this.methd;
event.preventDefault();
$.ajax({
url: this.action,
type: this.method,
data: $(this).serialize(),
dataType: "json",
success: function (result) {
debugger;
bindData(result);
}
});
return false;
});
var bindData = function (result) {
debugger;
alert('Glad this is kind-of working!');
$("#list").setGridParam({
datatype: 'jsonstring',
datastr: result,
caption: 'Suck It!'
}).trigger("reloadGrid");
}
更新...我将我的 bindData 函数更改为 Just setGridParam。并重新加载......仍然无法正常工作,但我认为我终于走上了正确的道路......下面有更多更新......
我错过了什么?我有我的 Json 数据,一切都应该正常吗?
更新:
我意识到我发送的是一个 JSON 字符串,而不是一个 JSON 对象......所以我将数据类型从 json 更改为 jsonstring......仍然不起作用。
这是返回我的 JSON 数据的内容。 更新到我现在正在尝试的内容:我想尝试将列名放入数据中......出于某种原因,我认为这可能会有所帮助
public static object JsonHelper(TemplateModel model){
var values = model.Template;
var JsonDataList = new {
total = 1,
page = 1,
records = model.Template.Count,
rows = (from val in values
select new {
cell = //new string(
"[\"id\" :\"" + val.EncounterId +",\""+
//"\"MRN\" :" +
"\"MRN\" :\"" + val.MRN + ",\"" +
//"\"Hospital_Fin\" :" +
"\"Hospital_Fin\" :\"" + val.HostpitalFinNumber + ",\"" +
//"\"First_Name\" :"+
"\"First_Name\" :\"" + val.FirstName + ",\"" +
//"\"Last_Name\" :" +
"\"Last_Name\" :\"" + val.LastName + ",\"" +
//"\"Date_of_birth\" :" +
"\"Date_of_birth\" :\"" + val.DateOfBirth.ToString() + ",\"" +
//"\"Completed_Pathway\" :" +
"\"Completed_Pathway\" :\"" + val.CompletedPathway + ",\"" +
//"\"Completed_Pathway_Reason\" :" +
"\"Completed_Pathway_Reason\" :\"" + val.CompletedPathwayReason + ",\"" +
//"\"PCP_Appointment\" :" +
"\"PCP_Appointment\" :\"" + val.PCPAppointmentDateTime.ToString() + ",\"" +
//"\"Specialist_Appointment\" :" +
"\"Specialist_Appointment\" :\"" + val.SpecialistAppointmentDateTime.ToString() + ",\"" +
//"\"Admit_Date\" :" +
"\"Admit_Date\" :\"" + val.AdminDate.ToString() + "\"]"
})//.ToArray()
};
return JsonDataList;
}
加...
return Json(DataRepository.JsonHelper(model.FirstOrDefault()), JsonRequestBehavior.AllowGet);
这就是我设置jqGrid的方式......
$(document).ready(function () {
$("#list").jqGrid({
shrinkToFit: false,
autowidth: true,
datatype: 'local',
mtype: 'POST',
colNames: ['Edit',
'MRN',
'Hospital Fin',
'First Name',
'Last Name',
'Date of birth',
'Completed Pathway',
'Completed Pathway Reason',
'PCP Appointment',
'Specialist Appointment',
'Admit Date'
],
colModel: [
{ name: 'Edit', width: 95, align: 'left' },
{ name: 'MRN', width: 125, align: 'left' },
{ name: 'Hospital_Fin', width: 145, align: 'left' },
{ name: 'First_Name', width: 115, align: 'left' },
{ name: 'Last_Name', width: 115, align: 'left' },
{ name: 'Date_of_birth', width: 145, align: 'left' },
{ name: 'Completed_Pathway', width: 125, align: 'left' },
{ name: 'Completed_Pathway_Reason', width: 165, align: 'left' },
{ name: 'PCP_Appointment', width: 115, align: 'left' },
{ name: 'Specialist_Appointment', width: 125, align: 'left' },
{ name: 'Admit_Date', width: 185, align: 'left' }],
rowNum: 10,
rowList: [5, 10, 20, 50],
sortname: 'Id',
sortorder: "desc",
viewrecords: true,
imgpath: '/Content/themes/UPMC-theme/images',
caption: 'My first grid'
}); })
我在网上某处读到将数据类型设置为“本地”,然后执行 setGridParam 和 trigger('reload') 会有所帮助。似乎合乎逻辑。但我仍然有点不确定这将如何工作......仍然没有工作,但我正在给它旧大学尝试......
UDPATE:进一步解释 我的 JsonHelper 只是试图写出一个有效的 JsonString ......有人有他们如何定期执行此操作的示例吗?每当我只使用标准的 Json ActionHelper 东西时......我想我可以制作一个不同的对象返回值,但这意味着我将不得不使用更多的 linq......这是另一天的冒险。
**UPDATE2:几个月过去了“你好。我已经被另一个项目分心了大约一个月。现在它开始结束了,这段代码又开始让我烦恼了。我觉得我很接近了,但到目前为止...
无论如何,这是我正在谈论的空 jqGrid 的示例...
如您所见,我有两行数据,但它们中的任何一行都没有通常应该包含的任何数据...
这是调试模式下的数据...
["id" :"2005,""MRN" :"840108105,""Hospital_Fin" :"999999999999985,""First_Name" :"BETTY,""Last_Name" :"WHITE,""Date_of_birth" :"1/18/1951 12:00:00 AM,""Completed_Pathway" :",""Completed_Pathway_Reason" :",""PCP_Appointment" :"6/12/2012 12:00:00 AM,""Specialist_Appointment" :"6/12/2012 12:00:00 AM,""Admit_Date" :"7/5/2012 12:00:00 AM"]
["id" :"2025,""MRN" :"840108105,""Hospital_Fin" :"789654123000123,""First_Name" :"BETTY,""Last_Name" :"WHITE,""Date_of_birth" :"1/18/1951 12:00:00 AM,""Completed_Pathway" :",""Completed_Pathway_Reason" :",""PCP_Appointment" :"1/1/0001 12:00:00 AM,""Specialist_Appointment" :"1/1/0001 12:00:00 AM,""Admit_Date" :"7/6/2012 12:00:00 AM"]
这直接来自我的 JSON 助手......它基本上从我返回的模型中创建了两个字符串对象......这就是 JSON 数据在绑定期间在客户端的样子......
{"total":1,"page":1,"records":2,"rows":[{"cell":"[\"id\" :\"2005,\"\"MRN\" :\"840108105,\"\"Hospital_Fin\" :\"999999999999985,\"\"First_Name\" :\"BETTY,\"\"Last_Name\" :\"WHITE,\"\"Date_of_birth\" :\"1/18/1951 12:00:00 AM,\"\"Completed_Pathway\" :\",\"\"Completed_Pathway_Reason\" :\",\"\"PCP_Appointment\" :\"6/12/2012 12:00:00 AM,\"\"Specialist_Appointment\" :\"6/12/2012 12:00:00 AM,\"\"Admit_Date\" :\"7/5/2012 12:00:00 AM\"]"},{"cell":"[\"id\" :\"2025,\"\"MRN\" :\"840108105,\"\"Hospital_Fin\" :\"789654123000123,\"\"First_Name\" :\"BETTY,\"\"Last_Name\" :\"WHITE,\"\"Date_of_birth\" :\"1/18/1951 12:00:00 AM,\"\"Completed_Pathway\" :\",\"\"Completed_Pathway_Reason\" :\",\"\"PCP_Appointment\" :\"1/1/0001 12:00:00 AM,\"\"Specialist_Appointment\" :\"1/1/0001 12:00:00 AM,\"\"Admit_Date\" :\"7/6/2012 12:00:00 AM\"]"}]}
光看我的数据,好像有点乱。我将尝试修复它是否会使它看起来更好。但如果您有任何其他建议,我肯定会很感激。