【发布时间】:2014-10-04 17:14:27
【问题描述】:
我正在尝试构建一个动态菜单,该菜单使用 Identity 2.0 角色来定义用户应该看到的菜单项。我是 C# 的新手,需要一些帮助!
出于我所做的目的,我将角色视为菜单项,用户可以看到或看不到,具体取决于分配给他们的角色。我有两个级别的菜单项——父菜单项(例如“报告”和子菜单项(例如“订单报告”、“收据报告”等)。
所以我的ApplicationRole 类看起来像这样:
public string Description { get; set; }
public string MenuTitle {get;set;}
public string MenuIcon { get; set; }
public string ControllerName { get; set; }
public string ActionName { get; set; }
public virtual ApplicationRole ParentRole { get; set; }
我将仅针对 ParentRole 不为空的用户存储角色。我想知道为登录用户检索此角色列表并呈现菜单。
所以,逻辑是这样的:
- 获取用户分配给他们的所有菜单项(角色)的列表。
- 使用此列表,使用
ParentRole获取不同的父角色列表(菜单的顶层)。 -
使用这两个列表,在下面填充我的视图模型:
public class NavigationViewModel { public int MenuId { get; set; } public string MenuName { get; set; } public string ControllerName { get; set; } public string ActionName { get; set; } public string MenuIcon { get; set; } public IEnumerable<NavigationViewModel> ChildMenuItems { get; set; } }
基本上,我正在创建一个包含顶级菜单和子菜单的菜单。
我的问题是,就查询数据库而言,我真的不知道从哪里开始。 理想情况下,我想要一个对 Role 表执行自联接的查询,以便在同一个查询中获取父角色,但是我是 C# 和 Linq 的新手,不知道从哪里开始。
任何建议都会很棒。
【问题讨论】:
标签: c# sql linq-to-entities asp.net-mvc-5 asp.net-identity