Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.49 KB | None | 0 0
  1. // when I click on this
  2.     $(".like_post").on("click", function () {
  3.         var PostId = $(this).attr("data-item");
  4.         console.log("You Liked post #" + PostId);
  5.         $.post("/Post/Like", PostId, function (data) {
  6.             if (data.Data.IsOk) {
  7.                 $(this).closest(".likes").html($(this).closest("likes").html() + 1);
  8.             }
  9.             else
  10.             {
  11.                 showMessage(data.Data.Title, data.Data.Message);
  12.             }
  13.         });
  14.     });
  15.  
  16. // I get an internal server error and the Controller Action (below) doesn't get hit
  17.  
  18. [HttpPost]
  19. public JsonResult Like(int postId)
  20. {
  21.     try
  22.     {
  23.         // create a new Like
  24.         Opinion Like = new Opinion()
  25.         {
  26.             Direction = true,
  27.             PostId = postId,
  28.             UserId = UserId
  29.         };
  30.  
  31.        
  32.         var PostOpinions = db.Opinions.Where(x => x.PostId == postId && x.UserId == UserId);
  33.         if (PostOpinions != null)
  34.         {
  35.             // add the Like only if the user hasn't already Liked the post
  36.             db.Opinions.Add(Like);
  37.             db.SaveChanges();
  38.  
  39.             return CreateResponseMessage(Json(new { IsOk = true, PostId = postId, Liked = true }), true);
  40.         }
  41.         else
  42.         {
  43.             // if the user has already Liked the post, remove the Like
  44.             db.Opinions.Remove(Like);
  45.             return CreateResponseMessage(Json(new { IsOk = true, PostId = postId, Liked = false }), true);
  46.         }                
  47.     }
  48.     // catch goes here
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement