【发布时间】:2014-05-14 13:56:07
【问题描述】:
我在使用 JTable 和 MVC 4 时遇到问题。 我没有将数据加载到表中,我不明白为什么: 我得到的错误非常普遍: 与服务器通信时出错
在服务器端调试代码我将数据加载到我的对象中,但将其映射到 Json 到 jtable 不起作用。
Customer.cs 类的片段:
public partial class customer
{
public customer()
{
}
...
[Display(Name = "Name")]
public string CUST_NAME { get; set; }
我的控制器类的片段:
[授权] 公共类 CustomerController : 控制器 { 私人 pcmEntities db = 新 pcmEntities();
#region JSON Ajax based CRUD
[HttpPost]
public JsonResult CustomerList()
{
try
{
List<customer> customers = db.customers.ToList();
return Json(new { Result = "OK", Records = customers }, JsonRequestBehavior.AllowGet );
}
catch (Exception ex)
{
return Json(new { Result = "ERROR", Message = ex.Message });
}
}
我的观点片段:
<div id="CustomerTable" style="width: 580px; margin: auto;">Customer Table</div>
<script type="text/javascript">
$(document).ready(function () {
//Prepare jtable plugin
$('#CustomerTable').jtable({
title: 'Customer List',
actions: {
listAction: '/Customer/CustomerList',
deleteAction: '/Customer/DeleteCustomer',
updateAction: '/Customer/UpdateCustomer',
createAction: '/Customer/CreateCustomer'
},
fields: {
CUST_NAME: {
title: 'Name',
width: '15%'
}
}
});
//Load customer list from server
$('#CustomerTable').jtable('load');
});
</script>
有人解决了吗?
最好的问候, 帕特里克
更新: 我解决了这个问题。如果有人有同样的问题,那就是序列化 Json 对象(EF)中的循环引用。当您在您的模型中具有类似 Parent=>Child=>Parent 的关系时,JSON 序列化失败。我解决的方法是在属性上使用 [ScriptIgnore(ApplyToOverrides=true)],这可能会导致循环引用例如:
[ScriptIgnore(ApplyToOverrides = true)]
public virtual ICollection<customer_documents> customer_documents { get; set; }
干杯!
【问题讨论】:
-
您确定 CustomerList 应该是 HttpPost 吗?我对jtable一无所知,但这看起来不对
-
是的,JTable 使用文档中的 HttpPost:jtable.org/Demo/PagingAndSorting,卡住并且不明白你做错了什么很烦人
标签: c# jquery asp.net-mvc-4 jquery-jtable