首页 > 解决方案 > 不同用户会话的相似记录数

问题描述

在我的 MVC 应用程序中,用户登录是通过会话进行的。存在一个仪表板,显示分配给每个用户的票数。此计数通过 SQL 过程启动。

ALTER PROC [dbo].[Usp_GetTicketCountByUserID]
    @UserID int
AS
BEGIN
    Select  AssignToAdmin,
        COUNT(CASE WHEN [Status] = 'A' then 1 else NULL END) openCount,
        COUNT(CASE WHEN [Status] = 'C' then 2 else NULL END) ClosedCount
    from AssignedRoles
    where AssignToAdmin = @UserID
    GROUP BY AssignToAdmin
END
GO

当我从 SQL 执行过程时,我得到了正确的结果,但是从应用程序中它显示零(0)。我注意到,当我删除 where 子句时,它会为所有用户显示类似的计数,这不应该是这种情况。

控制器:

var x = _ITimesheet.GetTimeSheetsCountByUserID(Convert.ToInt32(Session["UserID"]));

if (x != null)
{
    ViewBag.MyCall = x.OpenCount;
    ViewBag.ClosedTicket = x.ClosedCount;
}
else
{
    ViewBag.MyCall = 0;
    ViewBag.ClosedTicket = 0;
}

班级

public DisplayViewModel GetTimeSheetsCountByUserID(int UserID)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TSC"].ToString()))
    {
        var param = new DynamicParameters();
        param.Add("@UserID", UserID);
        return con.Query<DisplayViewModel>("Usp_GetTicketCountByUserID", param, null, true, 0, System.Data.CommandType.StoredProcedure).FirstOrDefault();
    }
}

任何有关如何解决此问题的帮助将不胜感激。

标签: c#sql-serverasp.net-mvc

解决方案


我想到了。显然我不恰当地分配了该类 [UserID] 而不是 [AdminID]。这是因为我有基于用户角色的各种会话。


推荐阅读