1

My app has a "Show all comments" similar to the one in Facebook. When the user clicks on the "show all" link, I need to update my list which initially has upto 4 comments with all comments. I'll show some code first and then ask some questions:

jQuery:
ShowAllComments = function (threadId) {
    $.ajax({
        type: "POST",
        url: "/Home/GetComments",
        data: { 'threadId': threadId },
        dataType: "json",
        success: function (result) {
            alert(result);
        },
        error: function (error) {
            alert(error);
        }
    });
};

Home Controller:
 // GET: /GetComments
 [HttpPost]
 public JsonResult GetComments(int threadId)
 {
     var comments = repository.GetComments(threadId).ToList();
      return Json(comments );
 }

Questions:

  1. When I tried GET instead of POST, I got this error: "This request has been blocked because sensitive information could be disclosed to third party web sites when this is used in a GET request. To allow GET requests, set JsonRequestBehavior to AllowGet." Is POST usually recommended instead of GET when making these ajax requests? If not, how do I get it to work with GET? Where do I set JsonRequestBehavior to AllowGet?

  2. After changing it to POST, I now get this error: A circular reference was detected while serializing an object of type 'Models.Thread'. I'm using Entity Framework 4, and I've read that adding a [scriptignore] attribute on the navigation property would resolve this, so I added a partial class of the entity with the property, it said "property is already defined". How do I then deal with this because I can't directly modify the code generated by EF4.

Prabhu
  • 12,995
  • 33
  • 127
  • 210
  • I don't know the answer, but can you try using POCO objects with EF4? That should get rid of any extra class members that may be causing this. – Nelson Rothermel Oct 07 '10 at 22:21
  • This was my first attempt with EF and MVC, so I went with the default. I'm hoping there's something I can do to fix this for now, as I need to get this site out, and I'll have to consider switching to POCO at some point. – Prabhu Oct 07 '10 at 22:28
  • That may not be true... usually with ORMs the POCOs get wrapped with stuff... its so the lazy loads work... which is likely whats causing this circular ref problem. – CrazyDart Oct 07 '10 at 22:29
  • I already [answered your second question](http://stackoverflow.com/questions/657939/serialize-entity-framework-objects-into-json/658056#658056). – Craig Stuntz Oct 08 '10 at 12:59

2 Answers2

2
  1. Set in the return Json. I would just use post, but if you want to make it hard on your self, use get.

    public JsonResult blah() { return Json("obj", JsonRequestBehavior.AllowGet); }

  2. It is true when most ORM objects get serialized the serialization tries to searlize the hidden goodies the ORM needs, AND (sounds like your case) all of the lazy load stuff... this causes bad mojo. Can I throw a suggestion out? Why not let MVC do what its good at and generate the view for you? You could just use the jQuery .load and use a view.

CrazyDart
  • 3,803
  • 2
  • 23
  • 29
  • So you mean instead of manipulating the DOM myself with the json result, just call the action method through .load to get the AJAX effect? – Prabhu Oct 07 '10 at 22:30
  • that is correct!! $("#resultdiv").load(url, { 'threadId': threadId }); should work out for you. – CrazyDart Oct 07 '10 at 22:33
  • I was thinking and this is another good reason for MVVC... View Models. You should have a simple View Model that just shows what you need put in the view or in this case return as a JSON object. Check out this post: http://stackoverflow.com/questions/657939/serialize-entity-framework-objects-into-json – CrazyDart Oct 07 '10 at 22:37
  • Ok, I will stop after this one... surly I have better things to do... For SEO reasons you could drop the threadId, and just use the routed url. So localhost/Comments/ShowAllFor/2345345 would be a much better thing if you want SEO to have a good day. – CrazyDart Oct 07 '10 at 22:45
  • Thanks for the suggestions...will give these a try – Prabhu Oct 07 '10 at 22:59
0

Answers:

  1. try return Json(comments, JsonRequestBehavior.AllowGet); There are good reasons for the default behavior though.
  2. for anything going down the wire, you are going to really want to create a very simple view model object rather than sending your domain entity down the wire. Lots of fancy things happen in the EntityFramework that don't work with serialization as you are finding out.
Wyatt Barnett
  • 15,573
  • 3
  • 34
  • 53