说一说MVC的MenuCard(五)

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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>@ViewBag.Title - 我的 ASP.NET 应用程序</title>
    @Styles.Render("~/Content/css")
    @Scripts.Render("~/bundles/modernizr")
</head>
<body>
    <div class="navbar navbar-inverse navbar-fixed-top">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                @Html.ActionLink("应用程序名称", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
            </div>
            <div class="navbar-collapse collapse">
                <ul class="nav navbar-nav">
                    <li>@Html.ActionLink("主页", "Index", "Home")</li>
                    <li>@Html.ActionLink("关于", "About", "Home")</li>
                    <li>@Html.ActionLink("联系方式", "Contact", "Home")</li>
                </ul>
            </div>
        </div>
    </div>
    <div class="container body-content">
        <div class="row">
            <div  class="col-md-3">
                @{ 
                    User loginuser = Session["User"] as User;
                    BookShopEntities DB = new BookShopEntities();
                    List<ModuleVM> list = new List<ModuleVM>();
                    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);
                        }
                    }
                }
                <ul class="list-group">
                    @foreach (ModuleVM module in list)
                    {
                        <li class="list-group-item">
                            @module.Name
                        <ul class="list-group">
                            @foreach (MenuVm menu in module.menuVms)
                            {
                                <li class="list-group-item">
                                    @menu.Name
                                <div class="list-group">
                                    @foreach (ActionVm a in menu.actions)
                                    {
                                        <a class="list-group-item" href="/@menu.ctrName/@a.ActName">@a.Name</a>
                                    }
                                </div>
                            </li>
                        }
                        </ul>
                    </li>
                }
                </ul>
            </div>
            <div class="col-md-12">@RenderBody()</div>
        </div>
        <hr />
        <footer>
            <p>&copy; @DateTime.Now.Year - 我的 ASP.NET 应用程序</p>
        </footer>
    </div>

    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/bootstrap")
    @RenderSection("scripts", required: false)
</body>
</html>

 

posted @ 2018-08-27 21:49  ZaraNet  阅读(136)  评论(2编辑  收藏  举报