将列表传递给它时,成功函数不会触发

时间:2019-06-11 04:52:14

标签: jquery json ajax asp.net-mvc model-view-controller

我是MVC,AJax和JSON的新手。我正在根据传递给控制器​​的参数创建字母列表

 public JsonResult RefreshTable(int fld_id)        
{            
    List<Referring> letterList = new List<Referring>();
    using (WebECartableEntities dc = new WebECartableEntities())
    {
        letterList = dc.Referrings.Where(u =>u.fld_Id == fld_id).ToList();
    }
    return Json(letterList, JsonRequestBehavior.AllowGet);    
}

letterList填充有字母列表。 然后像这样在jQuery Ajax中得到结果:

  var CategoryClick= function(clicked_id) {
       $.ajax({
           type: "Post",
           url: '/Main/RefreshTable',
           data: { fld_id: clicked_id },
           success: function (reutrnList) {
               alert("success");
           }
           ,
           error: function () {
               alert("Error");
           }
       });
    }

在这种情况下,尽管Controller Method工作正常,但我仍然收到“错误”消息。但是,如果我将Controller方法更改为此:

    public JsonResult RefreshTable(int fld_id)
    {           
        List<Referring> letterList = new List<Referring>();
        using (WebECartableEntities dc = new WebECartableEntities())
        {
            var items= dc.Referrings.Where(u =>u.fld_Id == fld_id).ToList();
            foreach (var item in items)
            {
                Referring reff = new Referring();
                reff.ID = item.ID;
                reff.SenderPosition = item.SenderPosition;
                reff.Subject = item.Subject;
                letterList.Add(reff);
            }
        }
        var reutrnList = letterList;
        return Json(reutrnList, JsonRequestBehavior.AllowGet);           

    }

现在一切正常,我收到成功消息。 谁能告诉我为什么这是第一种方法。我想同时拥有一个列表,并将其返回给jquery Ajax方法。不能一一列出。

谢谢

1 个答案:

答案 0 :(得分:1)

发生这种情况是由于延迟加载,您可以尝试通过将Configuration.LazyLoadingEnabled设置为false来禁用延迟加载。

更多内容

Solving "The ObjectContext instance has been disposed and can no longer be used for operations that require a connection" InvalidOperationException