0

I am new in MVC, AJax and JSON. I am creating a list of letteres according to a parameter passing to the controller like this

 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);    
}

the letterList is filled with list of letters. and I get the result in jquery Ajax like this:

  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");
           }
       });
    }

in this case I get the "Error" message although the Controller Method workis correctly. But if I change the Controller method to this:

    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);           

    }

Now Everything is fine and I get the Success message. can anyone tell me why is that in first method. I want to have a list simultaneously and return it to jquery Ajax method. not creating one by one list .

Thank you

nnmmss
  • 2,850
  • 7
  • 39
  • 67

1 Answers1

1

This is happening because of the lazy loading , you may try disabling lazy loading by setting Configuration.LazyLoadingEnabled to false .

More on,

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

Srinivasan Sekar
  • 2,049
  • 13
  • 22