Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // when I click on this
- $(".like_post").on("click", function () {
- var PostId = $(this).attr("data-item");
- console.log("You Liked post #" + PostId);
- $.post("/Post/Like", PostId, function (data) {
- if (data.Data.IsOk) {
- $(this).closest(".likes").html($(this).closest("likes").html() + 1);
- }
- else
- {
- showMessage(data.Data.Title, data.Data.Message);
- }
- });
- });
- // I get an internal server error and the Controller Action (below) doesn't get hit
- [HttpPost]
- public JsonResult Like(int postId)
- {
- try
- {
- // create a new Like
- Opinion Like = new Opinion()
- {
- Direction = true,
- PostId = postId,
- UserId = UserId
- };
- var PostOpinions = db.Opinions.Where(x => x.PostId == postId && x.UserId == UserId);
- if (PostOpinions != null)
- {
- // add the Like only if the user hasn't already Liked the post
- db.Opinions.Add(Like);
- db.SaveChanges();
- return CreateResponseMessage(Json(new { IsOk = true, PostId = postId, Liked = true }), true);
- }
- else
- {
- // if the user has already Liked the post, remove the Like
- db.Opinions.Remove(Like);
- return CreateResponseMessage(Json(new { IsOk = true, PostId = postId, Liked = false }), true);
- }
- }
- // catch goes here
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement