本次参考学习了
https://www.cnblogs.com/ymnets/p/3424309.htmlC
这个人的教程
但是由于时代久远,教程节奏快,版本的问题。对于初学者来说,很不友好。
正好我也要学习,所以自己重新写一下,顺带改一改其中的小bug。
系统目标:实现一个权限管理案例 用户—角色—权限
本节目标:导航菜单
1.准备工作
下载http://files.cnblogs.com/ymnets/wdtree.7z
解压后,把js文件放到Scripts目录下,把样式放到Content目录下
index页面把样式引入
<script src="~/Scripts/jquery.tree.js"></script>
<link href="~/Content/tree/css/tree.css" rel="stylesheet" />
js
$(function () {
var o = {
showcheck: false,
url: "/Home/GetTree",
onnodeclick: function (item) {
var tabTitle = item.text;
var url = "../../" + item.value;
var icon = item.Icon;
if (!item.hasChildren) {
addTab(tabTitle, url, icon);
} else {
$(this).parent().find("img").trigger("click");
}
}
}
$.post("/Home/GetTree", { "id": "0" },
function (data) {
if (data == "0") {
window.location = "/Account";
}
o.data = data;
$("#RightTree").treeview(o);
}, "json");
});
2.数据库
建表语句
USE [AppsDB]
GO
/****** Object: Table [dbo].[SysModule] Script Date: 2019/8/19 9:21:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[SysModule](
[Id] [varchar](50) NOT NULL,
[Name] [varchar](200) NOT NULL,
[EnglishName] [varchar](200) NULL,
[ParentId] [varchar](50) NULL,
[Url] [varchar](200) NULL,
[Iconic] [varchar](200) NULL,
[Sort] [int] NULL,
[Remark] [varchar](4000) NULL,
[State] [bit] NULL,
[CreatePerson] [varchar](200) NULL,
[CreateTime] [datetime] NULL,
[IsLast] [bit] NOT NULL,
[Version] [timestamp] NULL,
CONSTRAINT [PK_SysModule] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[SysModule] WITH NOCHECK ADD CONSTRAINT [FK_SysModule_SysModule] FOREIGN KEY([ParentId])
REFERENCES [dbo].[SysModule] ([Id])
GO
ALTER TABLE [dbo].[SysModule] NOCHECK CONSTRAINT [FK_SysModule_SysModule]
GO
插入数据
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('0','顶级菜单','Root','0','','',0,'',1,'Administrator','10 1 2012 12:00AM',0,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('BaseSample','模板样例','Sample by Ajax','SampleFile','SysSample','',0,'',1,'Administrator',NULL,1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('Document','我的桌面','Start','PersonDocument','Home/Doc','',0,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('Info','我的资料','Info','PersonDocument','Home/Info','',0,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('InfoHome','主页','Home','Information','MIS/Article','',1,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('Information','信息中心','Information','OA','','',0,'',1,'Administrator','10 1 2012 12:00AM',0,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('ManageInfo','管理中心','Manage Article','Information','MIS/ManageArticle','',4,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('ModuleSetting','模块维护','Module Setting','RightManage','SysModule','',100,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('MyInfo','我的信息','My Article','Information','MIS/MyArticle','',2,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('PersonDocument','个人中心','Person Center','0','','',2,'',1,'Administrator','10 1 2012 12:00AM',0,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('RightManage','权限管理','Authorities Management','0','','',4,'',1,'Administrator','10 1 2012 12:00AM',0,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('RoleAuthorize','角色权限设置','Role Authorize','RightManage','SysRight','',6,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('RoleManage','角色管理','Role Manage','RightManage','SysRole','',2,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SampleFile','开发指南样例','SampleFile','0','SysSample','',1,'',1,'Administrator',NULL,0,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemConfig','系统配置','System Config','SystemManage','SysConfig','',0,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemExcepiton','系统异常','System Exception','SystemManage','SysException','',2,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemJobs','系统任务','System Jobs','TaskScheduling','Jobs/Jobs','',0,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemLog','系统日志','System Log','SystemManage','SysLog','',1,'',1,'Administrator','10 1 2012 12:00AM',1,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('SystemManage','系统管理','System Management','0','','',3,'',1,'Administrator','10 1 2012 12:00AM',0,NULL)
INSERT INTO [SysModule] ([Id],[Name],[EnglishName],[ParentId],[Url],[Iconic],[Sort],[Remark],[State],[CreatePerson],[CreateTime],[IsLast],[Version]) values ('UserManage','系统管理员','User Manage','RightManage','SysUser',NULL,1,NULL,1,'Administrator','10 1 2012 12:00AM',1,NULL)
然后记得从EF更新数据模型
emmm
3.后台代码
修改HomeController代码
[Dependency]
public IHomeBLL homeBLL { get; set; }
public ActionResult Index()
{
return View();
}
/// <summary>
/// 获取导航菜单
/// </summary>
/// <param name="id">所属</param>
/// <returns>树</returns>
public JsonResult GetTree(string id)
{
List<SysModule> menus = homeBLL.GetMenuByPersonId(id);
var jsonData = (
from m in menus
select new
{
id = m.Id,
text = m.Name,
value = m.Url,
showcheck = false,
complete = false,
isexpand = false,
checkstate = 0,
hasChildren = m.IsLast ? false : true,
Icon = m.Iconic
}
).ToArray();
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
添加下面这些
IHomeRepository
public interface IHomeRepository
{
List<SysModule> GetMenuByPersonId(string moduleId);
}
HomeRepository
public class HomeRepository : IHomeRepository, IDisposable
{
public List<SysModule> GetMenuByPersonId(string moduleId)
{
using (DBContainer db = new DBContainer())
{
var menus =
(
from m in db.SysModule
where m.ParentId == moduleId
where m.Id != "0"
select m
).Distinct().OrderBy(a => a.Sort).ToList();
return menus;
}
}
public void Dispose()
{
}
}
IHomeBLL
public interface IHomeBLL
{
List<SysModule> GetMenuByPersonId(string moduleId);
}
HomeBLL
public class HomeBLL : IHomeBLL
{
[Dependency]
public IHomeRepository HomeRepository { get; set; }
public List<SysModule> GetMenuByPersonId(string moduleId)
{
return HomeRepository.GetMenuByPersonId(moduleId);
}
}
截图
4.总结
没哈总结的,最近有点懒,不喜欢自己