bootstrap adminlte教程3:实现菜单权限管理菜单树2 (完成新建菜单项。)
上个笔记已经实现了新建菜单,可我们真实的新建,是需要使用上级菜单的,这需要用到下拉菜单。
百度无数次,不管方法是否最好,已达到我们的需求。
1.在Create 的GET方法中,将现有菜单存为列表。
// GET: Menus/Create public ActionResult Create() { //var xxxx = db.Menus.ToList(); //ViewData["drolistmenu"] = db.Menus.Select(g => new SelectListItem ViewBag.drolistmenu = db.Menus.Select(g => new SelectListItem { Text = g.Name, Value = g.Id,Selected=false }); return View(); }
2.在前台使用:
@Html.DropDownList("ParentId", new SelectList(ViewBag.drolistmenu, "Value", "Text"),"请选择")
上面的ParentId就是我控件的Name值。
3,经测试,完全达到了我的要求。
4.为了方便,我要更改一些前台,达到漂亮统一的样式。
同时要加一个菜单,用rolename用参数传过去。
@Html.ActionLink("编辑", "Edit", new { id = role.Id }, new { @class = "btn btn-success" }) @Html.ActionLink("删除", "Del", new { id = role.Id }, new { @class = "btn btn-warning", onclick = "return confirm('你确定要删除吗?')" }) @Html.ActionLink("添加用户", "UserToRole", new { id = role.Id }, new { @class = "btn btn-primary" }) @Html.ActionLink("查看用户", "ViewRoleUser", new { id = role.Id }, new { @class = "btn btn-success" }) <a class="btn btn-warning" href="/Menus/Permissions?roleNamel=@role.Name">授权</a>
效果。
然后在Menus控制器中增加授 权的GET和POST方法。我先写了GET方法。
// GET: Menus //public async Task<ActionResult> Permissions(string roleNamel) public ActionResult Permissions(string roleNamel) { ViewBag.rolename = roleNamel; if (string.IsNullOrWhiteSpace(roleNamel)) { return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest); } return View(db.Menus.ToList()); //return View(await db.Menus.ToListAsync()); }
然后生成了视图。
@model IEnumerable<jsdhh2.Models.Menu> @{ ViewBag.Title = "角色关联用户"; Layout = "~/Views/Shared/_AdminLayout.cshtml"; } <!-- Content Header (Page header) --> <section class="content-header"> <h1></h1> <h1> @ViewBag.rolename <small>分配权限菜单</small> </h1> <ol class="breadcrumb"> <li><a href="#"><i class="fa fa-dashboard"></i> Level</a></li> <li class="active">Here</li> </ol> </section> <!--end Content Header (Page header) --> <!-- Main content --> <section class="content"> <a class="btn btn-primary" href="/Role/List">返回</a> | <a class="btn btn-warning" href="/Role/List">授权</a> <h1></h1> <div class="col-xs-12"> <div class="box"> <div class="box-header"> <h3 class="box-title">所有菜单请选择打上勾</h3> </div> <!-- /.box-header --> <div class="box-body table-responsive no-padding"> <table class="table table-hover"> <tbody> <tr> <th> @Html.DisplayNameFor(model => model.Id) </th> <th> @Html.DisplayNameFor(model => model.ParentId) </th> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Url) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Id) </td> <td> @Html.DisplayFor(modelItem => item.ParentId) </td> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Url) </td> <td></td> </tr> } </tbody> </table> </div><!-- /.box-body --> </div><!-- /.box --> </div> <!-- Main content --> </section>
然后看效果。
但是我们要按一级,一级下的菜单,再二级这样显示, 这就需要用到遍历,还需要用到对象,这个我确实不懂,也不学,哈,我就偷懒 用了排序来解决。一级就1,11,12,另一个一级就21,22,23, 这样,然后改写控制器
// GET: Menus
//public async Task<ActionResult> Permissions(string roleNamel)
public ActionResult Permissions(string roleNamel)
{
ViewBag.rolename = roleNamel;
if (string.IsNullOrWhiteSpace(roleNamel))
{
return new HttpStatusCodeResult(System.Net.HttpStatusCode.BadRequest);
}
var listMenu = db.Menus.OrderBy(s => s.Order);
return View(listMenu.ToList());
//return View(await db.Menus.ToListAsync());
}
显示效果。