1.数据库设计
1 2 create database BookShop 3 go 4 5 use bookshop 6 go 7 8 --模块表 9 create table Module 10 ( 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 -- 功能表 Menu 18 create table Menu 19 ( 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, -- 对应的 控制器名称 BookController 24 ModuleID int not null foreign key references Module(ModuleID) -- 25 ) 26 27 -- 方法函数表 Action 28 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 UserRole 64 add constraint PK_UserRole primary key (RoleID,UserID) 65 66 go 67 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 RoleAction 75 add constraint PK_RoleAction primary key (RoleID,ActionID)
2.实体类
public class ModuleVM { public ModuleVM() { menuVms = new List<MenuVm>(); } public string ID { get; set; } public string Name { get; set; } public List<MenuVm> menuVms { get; set; } } public class MenuVm { public MenuVm() { actions = new List<ActionVm>(); } public int ID { get; set; } public string Name { get; set; } public string ctrName { get; set; } public List<ActionVm> 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 "Content-Type" content="text/html; charset=utf-8"/> "utf-8" /> "viewport" content="width=device-width, initial-scale=1.0">@ViewBag.Title - 我的 ASP.NET 应用程序 @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr")class="navbar navbar-inverse navbar-fixed-top">class="container">class="navbar-header"> @Html.ActionLink("应用程序名称", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })class="navbar-collapse collapse">class="nav navbar-nav">
- @Html.ActionLink("主页", "Index", "Home")
- @Html.ActionLink("关于", "About", "Home")
- @Html.ActionLink("联系方式", "Contact", "Home")
class="container body-content">@Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false)class="row">class="col-md-3"> @{ 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); } } } class="list-group"> @foreach (ModuleVM module in list) {
- class="list-group-item"> @module.Name
}class="list-group"> @foreach (MenuVm menu in module.menuVms) {
- class="list-group-item"> @menu.Name
}class="list-group"> @foreach (ActionVm a in menu.actions) { class="list-group-item" href="/@menu.ctrName/@a.ActName">@a.Name }class="col-md-12">@RenderBody()