Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to build my social network like Twitter. I wanna to like with JSON. I have model that type is List. I need get Tweet Id to like method but I can't. I can like first tweet,That get first tweet Id in Homepage , then that can get others tweet Id. If i dont like first tweet in HomePage, I can't like other tweets. Please help me.

C#
<pre> public async Task<IActionResult> AddLike(Guid id)
        {
            JsonLikeVM js = new JsonLikeVM();
            Tweet tweet = uow.Tweet.GetById(id);
            AppUser user = await userManager.FindByNameAsync(User.Identity.Name);
            if (!(uow.Like.Any(x => x.UserId == user.Id && x.TweetId == tweet.Id)))
            {
                Like like = new Like();
                like.TweetId = tweet.Id;
                like.UserId = user.Id;
                uow.Like.Add(like);
                uow.SaveChange();
                js.likes = uow.Like.FindByList(x => x.TweetId == tweet.Id).Count();
                return new JsonResult(js);
            }
            else
            {
                Like like = uow.Like.Find(x => x.TweetId == tweet.Id && x.UserId == user.Id);
                uow.Like.Delete(like);
                uow.SaveChange();
                js.likes = uow.Like.FindByList(x => x.TweetId == tweet.Id).Count();
                return new JsonResult(js);
            }
        }


JavaScript
$('#like').click(function () {
           var id = document.getElementById("item").value;
               $.ajax({
               type: 'POST',
               url: '/Member/Like/Addlike/' + id,
               success: function (result) {
                   var s = result.likes + ' ' + 'Like';
                   $('#result').html(s);
               }
           });



       });


HTML
<pre> <a id="like">
            <input type="hidden" value="@item.Id" id="item" />
            <svg class="bi bi-heart" width="1em" height="1em" viewBox="0 0 20 20" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
                <path fill-rule="evenodd" d="M10 4.748l-.717-.737C7.6 2.281 4.514 2.878 3.4 5.053c-.523 1.023-.641 2.5.314 4.385.92 1.815 2.834 3.989 6.286 6.357 3.452-2.368 5.365-4.542 6.286-6.357.955-1.886.837-3.362.314-4.385-1.114-2.175-4.2-2.773-5.883-1.043L10 4.748zM10 17C-5.333 6.868 5.279-1.04 9.824 3.143c.06.055.119.112.176.171a3.12 3.12 0 01.176-.17C14.72-1.042 25.333 6.867 10 17z" clip-rule="evenodd"></path>
            </svg>
        </a>
        @item.Likes.Count()


What I have tried:

I tried many things, I cant. What can I do ?
Posted
Updated 10-Mar-20 9:33am
v2

1 solution

IDs in an HTML document must be unique. From your description, you're using the same block of HTML for multiple items, which is generating multiple elements with the same ID.

You'll need to change the way you generate and process your HTML.
Razor
<a class="like" href='@Url.Action("AddLike", "Like", new { item.Id })'>
    <svg ...>
        ...
    </svg>
    <span class="like-count">@item.Likes.Count()</span> likes
</a>
JavaScript
$(document).on("click", "a.like", function(e){
    e.preventDefault();
    
    var me = this;
    $.post(this.href).then(function(result){
        $(me).find("span.like-count").text(result.likes);
    });
});
 
Share this answer
 
Comments
Member 14769019 10-Mar-20 15:37pm    
Thank you it's working!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900