JTable MVC 4 - 与服务器通信时发生错误

时间:2014-05-14 13:56:07

标签: c# jquery asp.net-mvc-4 jquery-jtable

使用JTable和MVC 4时遇到问题。 我没有将我的数据加载到表中,我不明白为什么: 我得到的错误是非常通用的: 与服务器通信时发生错误

调试服务器端的代码我将数据加载到我的对象中,但将其映射到Json到jtable不起作用。

Customer.cs类的片段:

public partial class customer
{
    public customer()
    {

    }

    ...

    [Display(Name = "Name")]
    public string CUST_NAME { get; set; }

我的控制器类的片段:

[授权]     public class CustomerController:Controller     {         private pcmEntities db = new 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)的循环引用。当你的ur模型中的关系像Parent =&gt; Child =&gt; Parent时,JSON序列化失败。我解决的方法是在属性/ s上使用[ScriptIgnore(ApplyToOverrides = true)],它可能导致循环引用ex:

[ScriptIgnore(ApplyToOverrides = true)]
public virtual ICollection<customer_documents> customer_documents { get; set; }

干杯!

1 个答案:

答案 0 :(得分:1)

今天得到了同样的错误。 修复

 listAction: '/Customer/CustomerList',
 deleteAction: '/Customer/DeleteCustomer',
 updateAction: '/Customer/UpdateCustomer',
 createAction: '/Customer/CreateCustomer'

作为

 listAction: 'Customer/CustomerList',
 deleteAction: 'Customer/DeleteCustomer',
 updateAction: 'Customer/UpdateCustomer',
 createAction: 'Customer/CreateCustomer'

这应该有效