1.数据库设计
1 2 create database BookShop 3 go 4 5 use bookshop 6 go 7 8 --模块表 9 create table Module10 (11 ModuleID int not null primary key identity(1,1),12 ModuleName varchar(50) not null unique,13 ModuleIcon varchar(20) not null default '',14 )15 16 17 -- 功能表 Menu18 create table Menu19 (20 MenuID int not null primary key identity(1,1),21 MenuName varchar(50) not null unique,22 MenuIcon varchar(20) not null default '',23 CtrlName varchar(50) not null, -- 对应的 控制器名称 BookController24 ModuleID int not null foreign key references Module(ModuleID) --25 )26 27 -- 方法函数表 Action28 create table [Action]29 (30 ActionID int not null primary key identity(1,1),31 ActionName varchar(50) not null unique,32 ActionIcon varchar(20) not null default '',33 ActName varchar(50) not null , 34 MenuID int not null foreign key references Menu(MenuID) --35 )36 37 38 39 create table [Role]40 (41 RoleID int not null primary key identity(1,1),42 RoleName varchar(20) not null unique,43 [Description] varchar(100) not null default '没描述'44 )45 46 create table [User]47 (48 UserID int not null primary key identity(1,1), 49 LoginName varchar(20) not null unique, --登录名50 UserName varchar(20) not null , --真实姓名51 [Password] varchar(20) not null,52 53 )54 55 56 create table UserRole --用户和角色的关系表57 (58 RoleID int not null foreign key references [Role](RoleID),59 UserID int not null foreign key references [User](UserID) 60 )61 62 63 alter table UserRole64 add constraint PK_UserRole primary key (RoleID,UserID)65 66 go67 68 create table RoleAction -- 角色 方法 关系表69 (70 RoleID int not null foreign key references [Role](RoleID),71 ActionID int not null foreign key references [Action](ActionID) 72 )73 74 alter table RoleAction75 add constraint PK_RoleAction primary key (RoleID,ActionID)
2.实体类
public class ModuleVM { public ModuleVM() { menuVms = new List(); } public string ID { get; set; } public string Name { get; set; } public List menuVms { get; set; } } public class MenuVm { public MenuVm() { actions = new List (); } public int ID { get; set; } public string Name { get; set; } public string ctrName { get; set; } public List actions { get; set; } } public class ActionVm { public int ID { get; set; } public string Name { get; set; } public string ActName { get; set; } }
3.模板页面代码
@using PowerDemo.Models@using Action= PowerDemo.Models.Action@ViewBag.Title - 我的 ASP.NET 应用程序 @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr")@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false)@{ User loginuser = Session["User"] as User; BookShopEntities DB = new BookShopEntities(); Listlist = new List (); foreach (Role role in loginuser.Role) { foreach (Action action in role.Action) { MenuVm mvm = new MenuVm() { ID = action.Menu.MenuID, Name = action.Menu.MenuName, ctrName = action.Menu.CtrlName.Replace("Controller","") }; ActionVm avm = new ActionVm() { ID = action.ActionID, Name = action.ActionName, ActName = action.ActName }; mvm.actions.Add(avm); ModuleVM module = list.FirstOrDefault(mo => mo.ID==action.Menu.Module.ModuleID.ToString()); if (module==null) { module = new ModuleVM() { ID = action.Menu.Module.ModuleID.ToString(), Name = action.Menu.Module.ModuleName, }; list.Add(module); } module.menuVms.Add(mvm); } } } @foreach (ModuleVM module in list) {
- @module.Name
}@foreach (MenuVm menu in module.menuVms) {
- @menu.Name
}@foreach (ActionVm a in menu.actions) { @a.Name }@RenderBody()