MVC显示基于当前用户的数据

时间:2017-10-30 01:13:49

标签: asp.net-mvc authentication user-controls

我在MVC中有一个表有多对多的关系,表中的内容是{ SubjectID,UserID,LevelID} 现在如何过滤我希望当前用户登录的表的结果只能看到属于当前用户的 SubjectID,LevelId 。任何简单的想法使这项工作?谢谢并感谢您的回复..

控制器:

    [Authorize(Roles ="Teacher")]
    public ActionResult Index()
    {
        var subjectTeachers = db.SubjectTeachers.Include(s => s.Levels).Include(s => s.Subjects).Include(s => s.Users);
        return View(subjectTeachers.ToList());
    }

型号:

 [Key]
    public int SubTech { get; set; }

    public int SubjectID { get; set; }
    public virtual Subject Subjects { get; set; }

    public int UserID { get; set; }
    public virtual User Users { get; set; }


    public int LevelID { get; set; }
    public virtual Level Levels { get; set; }

1 个答案:

答案 0 :(得分:0)

由于您不想使用ASP.NET标识,因此可以尝试通过以下方式替换您的行:

[Authorize(Roles ="Teacher")]
public ActionResult Index()
{
    var subjectTeachers = db.SubjectTeachers.Include(s => s.Levels).Include(s => s.Subjects).Include(s => s.Users).Where(u => u.UserID == id);
    return View(subjectTeachers.ToList());
}

您应该从任一会话或通过控制器传递UserID,它应该可以工作。