我想構建一個使用Identity 2.0角色定義用戶應該看到的菜單項的動態菜單。我對C#很新,需要一些幫助!使用Linq to Entities獲取用戶具有自我加入的角色列表
爲了達到我所做的目的,我將角色看作是菜單項,用戶可以根據分配給他們的角色看到或不看到菜單項。我有兩個級別的菜單項 - 父菜單項(如「報告」和子菜單項(如「訂單報告」,「收據報告」等)
所以我的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不爲null。我想知道檢索登錄的用戶角色的列表和渲染菜單。
因此,邏輯將會是這樣的:
- 獲取用戶分配給他們的所有菜單項(角色)的列表。
- 使用此列表,請使用
ParentRole
獲取父級角色的不同列表(菜單的頂層)。 使用這些2所列出,填寫以下我的視圖模型:
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; } }
基本上,我創建了一個頂級菜單和子菜單的菜單。
我的問題是,我真的不知道從哪裏開始查詢數據庫。 理想情況下,我想要一個自加入到角色表的查詢來獲得相同查詢中的父角色,但是我是C#和Linq的新手,並且不知道從哪裏開始。
任何建議將是偉大的。