0

I'm making a "Like" button in a simple comment database MVC program. I'm passins the ID of the comment through to a ActionResult in the HomeController when I hover over the "Like" button. The problem (I think) is that I don't know how to pass the IEnumerable list of Likes to the ajax.

The script and HTML part:

HTML:

<a href="#" class="likes" title="No likes yet." id="@comment.ID">Like this</a>

Script:

$(".likes").hover(function (event) {

    var Liker = { "CID": event.target.id };

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Home/ShowLike/",
        data: JSON.stringify(Liker),
        dataType: "json",
        success: function (data) {
            $.each(data.Name, function (value) {
                alert(value);
            });
        },
        error: function (xhr, err) {
            // Note: just for debugging purposes!
            alert("readyState: " + xhr.readyState +
            "\nstatus: " + xhr.status);
            alert("responseText: " + xhr.responseText);
        }
    });

});

HomeController -> ShowLike

    [HttpPost]
    public ActionResult ShowLike(Liker ids)
    {
        LikesRepository lkrep = new LikesRepository();

        IEnumerable<Like> list = lkrep.GetLikes(ids.CID);

        return Json(list);
    }

LikesRepository

 public class LikesRepository
 {
    CommentDBDataContext m_db = new CommentDBDataContext();

    public IEnumerable<Like> GetLikes(int iden)
    {
        var result = from c in m_db.Likes
                     where c.CID == iden
                     orderby c.Name ascending
                     select c;

        return result;
    }

    public void AddLike(Like c)
    {
        m_db.Likes.InsertOnSubmit(c);
        m_db.SubmitChanges(); //This works
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
Mappan
  • 2,537
  • 2
  • 22
  • 27
  • Everything else looks fine for returning the IEnumerable. You should use a JS debugger to view the data property in your ajax success function and it will give you an understanding of how to handle the data upon return (assuming your data is not empty). I like using chrome and setting a breakpoint in my success function and then hover over the data object. – Zach Apr 09 '13 at 16:49

1 Answers1

0

After diving into the problem more, we found it was actually triggering an internal server error (500). This was caused by a circular reference from serializing the LINQ to SQL objects to JSON. This issue is discussed several times...

How to remove circular reference in Entity Framework?

How did I solve the Json serializing circular reference error?

Circular Reference exception with JSON Serialisation with MVC3 and EF4 CTP5w

An alternate solution is to return the data as lists of strings as they only required the names.

Community
  • 1
  • 1
Zach
  • 3,157
  • 1
  • 19
  • 32
  • By passing in data.Name into your each, your trying to pass in the Name property of a js Array object. You're data should already be an array. – Zach Apr 09 '13 at 16:46
  • The problem is that it never makes it into the success function. It goes into ShowLike and makes a new Enumerable and when it hits the return, it goes straight to error so it doen's go to Success. – Mappan Apr 09 '13 at 16:50
  • Oh I see... you're trying to pass an array FROM the js to the server, correct? – Zach Apr 09 '13 at 16:52
  • Can you put a breakpoint in your ShowLike to make sure that's being hit and the ids are populated? – Zach Apr 09 '13 at 16:58
  • First I do: var Liker = { "CID": event.target.id }; which I send to the ShowLike to map over with the Like class in the database. That works. I then retrieve all Likes with the CID == to that which I sent in. I then return them with return Json(list) and hope to catch them in the ajax. – Mappan Apr 09 '13 at 16:58
  • ids, like Enumerable and are both populated. This works when there are no likes on the comment. That's to say when the list is empty, it goes fine but when the list has some values(s) it just goes straight to the error function. – Mappan Apr 09 '13 at 16:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27885/discussion-between-zach-and-zanii) – Zach Apr 09 '13 at 17:01