Asp.net core 在ASP.NET内核中使用signer自定义实现like按钮

Asp.net core 在ASP.NET内核中使用signer自定义实现like按钮,asp.net-core,signalr,Asp.net Core,Signalr,为什么大家, 抱歉,如果我的问题与某些问题重复,但我没有找到解决问题的方法 然而,我正在尝试在我的asp.net核心mvc应用程序中为帖子实现自定义的like按钮,一切正常,除了当我点击like按钮时,它会改变颜色(比如在facebook上,蓝色等等),不仅仅是当前登录用户,而是所有登录用户。但当其他用户刷新窗口时,一切都恢复正常 我想要的是,当我点击“喜欢”按钮时,为每个人更新“喜欢计数器”,并为我更改颜色(当我某天回来时,我知道我喜欢那个帖子。) 有人知道如何做到这一点吗? 这是我的密码 I

为什么大家,

抱歉,如果我的问题与某些问题重复,但我没有找到解决问题的方法

然而,我正在尝试在我的asp.net核心mvc应用程序中为帖子实现自定义的like按钮,一切正常,除了当我点击like按钮时,它会改变颜色(比如在facebook上,蓝色等等),不仅仅是当前登录用户,而是所有登录用户。但当其他用户刷新窗口时,一切都恢复正常

我想要的是,当我点击“喜欢”按钮时,为每个人更新“喜欢计数器”,并为我更改颜色(当我某天回来时,我知道我喜欢那个帖子。)

有人知道如何做到这一点吗? 这是我的密码

Index.cshtml

@using Fitness_Centar.Data
@using Fitness_Centar.Data.Models
@using Fitness_Centar.Web.Areas.ModulClan.ViewModels
@using Fitness_Centar.Web.Helper
@model List<TimelineVM>

@{
    ViewData["Title"] = "Index";
    MyContext _ctx = (MyContext)ViewData["_ctx"];

User u = Context.GetLoggedUser();
int userId = -1;
if (u.Coach != null)
{
    userId = u.Coach.CoachId;
}
if (u.Employee != null)
{
    userId = u.Employee.EmployeeId;
}
if (u.Member != null)
{
    userId = u.Member.MemberId;
}
}

<div class="row">
<div class="col-sm-12 col-md-9 col-lg-9">
    <div class="box box-info custom aktivnosti">
        <div class="box-header with-border padding-l-25">
            <h3 class="box-title">Activities</h3>
        </div>

        <div class="box-body">
            @foreach (var p in Model)
            {
                <div id="@p.Post.PostClanovaId" class="post">
                    <div class="user-info">
                        <img class="img-circle img-bordered-sm" src="~/AdminLTE/dist/img/avatar5.png" />
                        <div class="desc">
                            <span class="username">@p.Post.User.Name @p.Post.User.LastName</span>
                            <span class="post-description">Published: @p.Post.PostDate</span>
                        </div>
                    </div>
                    <div class="post-content text-justify">
                        <p>@p.Post.Content</p>
                    </div>
                    <div class="post-footer">
                        @{int n = p.Post.PostId + 1594;}
                        @{
                            var c = _ctx.Likes.Where(x => x.UserId == userId && x.PostId == p.Post.PostId).FirstOrDefault();

                            if (c != null && c.UserId == userId)
                            {
                                <a href="" id="post-@n" class="likeButton link-black liked" data-postid="@p.Post.PostId" data-userid="@userId">
                                    <i class="fa fa-thumbs-o-up margin-r-5"></i>
                                    Like
                                </a>
                            }
                            else
                            {
                                <a href="" id="post-@n" class="likeButton link-black" data-postid="@p.Post.PostId" data-userid="@userId">
                                    <i class="fa fa-thumbs-o-up margin-r-5"></i>
                                    Like
                                </a>
                            }
                        }
                        <div class="num-of-likes" id="post-@n-numOfLikes">
                            @p.Likes.Count()
                            <span class="usersThatLiked">
                                @{
                                    string g = "";
                                    foreach (var l in p.Likes)
                                    {
                                        g += l.User.Ime + " " + l.Clan.Prezime;
                                    }
                                    @g;
                                }
                            </span>
                        </div>
                        <div class="comment pull-right">
                            <a href="#" class="link-black">
                                <i class="fa fa-comments-o margin-r-5"></i>
                                Comments
                            </a>
                        </div>
                        <div class="input-group">
                            <input type="text" class="form-control" placeholder="Komentiraj...">
                            <span class="input-group-btn">
                                <button type="button" class="btn btn-info btn-flat"><i class="fa fa-check"></i></button>
                            </span>
                        </div>
                    </div>
                </div>
            }
        </div>
    </div>
</div>
LikeHub.cs

public class LikeHub : Hub
{
    private readonly MyContext _ctx;
    public LikeHub (MyContext context)
    {
        _ctx = context;
    }

    public async Task SetLike(int userId, int postId)
    {
        Likes l = new Likes();
        Likes temp = _ctx.Likes.Where(x => x.PostId == postId && x.UserId == userId).FirstOrDefault();

        if(temp != null)
        {
            _ctx.Likes.Remove(temp);
        }
        else
        {
            _ctx.Likes.Add(l);

            l.UserId = userId;
            l.PostId = postId;
        }

        _ctx.SaveChanges();

        int numOfLikes= _ctx.Likes.Where(x => x.PostId == postId).Count();

        await Clients.All.SendAsync("ReceiveMessage", numOfLikes, postId);
    }
}

您可以通过

var userId=$(this.attr(“数据userId”)(就像有人单击类似按钮时所做的那样)

但问题是,当
SetLike
成功时(在
on ReceiveMessage
javascript函数中),您没有检查
用户ID

因此,要解决此问题,您需要将单击类似按钮的用户的
用户ID
从后端发送到前端(发送到打开的
(“接收消息”)
功能)

e、 g.:
wait Clients.All.SendAsync(“ReceiveMessage”,numOfLikes,postId,userId)

并在
ReciveMessage
上检查您从后端获得的
userId
是否与通过
var currentUserId=$(this).attr(“数据userId”)浏览的当前用户匹配

如果它们相等,则为
like按钮上色,否则只需更新
like计数器

这样可以解决你的问题



另一种解决方案是发送两个请求,一个是针对单击类似按钮的用户的请求,另一个是针对每个人更新类似计数器的请求。您可以通过

var userId=$(this.attr(“数据userId”)(就像有人单击类似按钮时所做的那样)

但问题是,当
SetLike
成功时(在
on ReceiveMessage
javascript函数中),您没有检查
用户ID

因此,要解决此问题,您需要将单击类似按钮的用户的
用户ID
从后端发送到前端(发送到打开的
(“接收消息”)
功能)

e、 g.:
wait Clients.All.SendAsync(“ReceiveMessage”,numOfLikes,postId,userId)

并在
ReciveMessage
上检查您从后端获得的
userId
是否与通过
var currentUserId=$(this).attr(“数据userId”)浏览的当前用户匹配

如果它们相等,则为
like按钮上色,否则只需更新
like计数器

这样可以解决你的问题



另一种解决方案是发送两个请求,一个是针对单击了类似按钮的用户的请求,另一个是针对每个人更新类似计数器的请求,我认为您应该将问题重新标记为signalr core,而不是mvc,因为代码主要是SignalI,我认为您应该将问题重新标记为signalr core而不是mvc,因为代码主要是信号,你需要你的答案。我更新了我的代码,现在运行良好:没有问题,我很高兴我帮了忙!:谢谢你的回答。我更新了我的代码,现在运行良好:没有问题,我很高兴我帮了忙!:D
public class LikeHub : Hub
{
    private readonly MyContext _ctx;
    public LikeHub (MyContext context)
    {
        _ctx = context;
    }

    public async Task SetLike(int userId, int postId)
    {
        Likes l = new Likes();
        Likes temp = _ctx.Likes.Where(x => x.PostId == postId && x.UserId == userId).FirstOrDefault();

        if(temp != null)
        {
            _ctx.Likes.Remove(temp);
        }
        else
        {
            _ctx.Likes.Add(l);

            l.UserId = userId;
            l.PostId = postId;
        }

        _ctx.SaveChanges();

        int numOfLikes= _ctx.Likes.Where(x => x.PostId == postId).Count();

        await Clients.All.SendAsync("ReceiveMessage", numOfLikes, postId);
    }
}