简体   繁体   English

ASP.NET MVC-根据用户角色获取数据库数据

[英]ASP.NET MVC - Get database data depending user role

I'm fairly new to the MVC and database stuff. 我对MVC和数据库知识还很陌生。

What I need to do is retrieve data depending on a user's role. 我需要做的是根据用户角色检索数据。 I can get all the data from the table below but I need to only show specific data. 我可以从下表中获取所有数据,但只需要显示特定数据即可。

tableId      roleId        data1        data2        data3
  1            1         some data    some data    some data
  2            1         some data    some data    some data 
  3            1         some data    some data    some data
  4            2         some data    some data    some data 
  5            2         some data    some data    some data
  6            3         some data    some data    some data

So if a user is logged in and assigned a role, how would I fetch data from the table for that role? 因此,如果用户已登录并分配了角色,我该如何从表中获取该角色的数据? If I am assigned roleId 1, I should only see data from the table with roleId = 1. 如果为我分配了roleId 1,则应该只从表中看到roleId = 1的数据。

I'm still learning and this would be awesome help. 我仍在学习,这将是很棒的帮助。

THANKS! 谢谢!

My Code: 我的代码:

Controller 控制者

    private projectEntities db = new projectEntities();

    // GET: /Assessment/
    public ActionResult Index()
    {
        //return View(db.Scales.ToList());
        var data = new ModelServices().GetScale();
        return View(data);


    }

Model 模型

public class ModelServices : IDisposable
{
    private readonly projectEntities entities = new projectEntities();
    public IEnumerable GetScale()
    {
        return entities.Scales.ToList();
    }
    public void Dispose()
    {
        entities.Dispose();
    }
}

View: 视图:

@model IEnumerable<project.Models.Scale>
@{
WebGrid grid = new WebGrid(Model);   
}

@grid.GetHtml()

I'd create a new services method like so: 我将创建一个新的服务方法,如下所示:

public IEnumerable<Scale> GetScalesWithRole(int roleId)
{
    return entities.Scales.Where(s => s.RoleId == roleId).ToList();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM