Prabhu
Prabhu

Reputation: 13325

Help with using jQuery with ASP.NET MVC

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.

Upvotes: 1

Views: 356

Answers (2)

CrazyDart
CrazyDart

Reputation: 3801

  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.

Upvotes: 2

Wyatt Barnett
Wyatt Barnett

Reputation: 15673

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.

Upvotes: 0

Related Questions