bootstrap adminlte教程3:实现菜单权限管理菜单树2 (对角色赋菜单权限) 新-mvc5 操作checkbox
上一章我使用了AJAX对角色进行赋值,确实成功了,但出了一个大问题,便宜是ajax不能跳转,我赋完值,那个checkbox选择画面一不变,这不是我要的效果呀,
我要赋完值后进行跳到角色列表。
下面进行更改。
1.更改赋角色的视图:Permissions的说明
@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"> @using (Html.BeginForm("Permissions1","Menus", new { id = ViewBag.rolename }, FormMethod.Post)) ///这里一定要注意,我当时把new{}放在了最后,老是传的null,这个写法才能真实传值,到现在也没明白, { <a class="btn btn-primary" href="/Role/List">返回</a> @*<button id="btn11" class="btn btn-info">授权</button>*@ @*@Html.ActionLink("授权", "Permissions1", new { roleName = ViewBag.rolename}, new { @class = "btn btn-primary" })*@ <input type ="submit" class="btn btn-warning" value="授权" /> ///用input type=submit 进行提交。 <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><input id="selectAll" name="selectAll" type="checkbox" value="true" onclick="changeState(this.checked)" /><input name="selectAll" type="hidden" value="false" />选择</th> <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> @*<input type="checkbox" value="@item.Id" class="chk" />*@ @*<input name="ids111" type="checkbox" id="@item.Id" value="@item.Id" />*@ @Html.CheckBox(item.Id,false) //播入一个checkbox列表 </td> <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> @section Scripts { @*<script type="text/javascript"> ///这个JS是ajax提前的,我注释掉了 $(function () { $('#btn11').click(function () { //取值 let arr = []; $('.chk:checked').each((i, e) => { arr.push(e.value); }); console.log(arr); //提交 $.post("/Menus/Permissions1", { ids: arr, rolename:'@ViewBag.rolename'}, (res) => { console.log(res); }); }) }) </script>*@ <script type="text/javascript"> function changeState(isChecked) { var chk_list = document.getElementsByTagName("input"); for (var i = 0; i < chk_list.length; i++) { if (chk_list[i].type == "checkbox") { chk_list[i].checked = isChecked; } } } </script> }
2. 控制器的写法
[HttpPost] ///注意post方法 public ActionResult Permissions1(string id,FormCollection form ) //定义二个变量,其实id应该为rolename,但我测试路由的时候没改,他就代表rolename { ViewBag.rolename = id; //传进来的角色名 var menuroles = db.MenuRoles.Where(c => c.RoleName == id); db.MenuRoles.RemoveRange(menuroles); //删除当rolename值为传进来的ID值时 var winnars = from x in form.AllKeys //定义所有的form值。 where form[x] != "false" select x; foreach (var id2 in winnars) //foreach所有的form值。 { if (id2 != "selectAll") //全选的那个不算数。那个checkbox值要去掉 { var addroleMenusViewModel = new MenuRole(); //建模 addroleMenusViewModel.RoleName = id; //模型的角色名=传进来的ID值 addroleMenusViewModel.MenuId = id2; //模型的MenuId值等于提交的Value值 db.MenuRoles.Add(addroleMenusViewModel); //新建Add命令 } } db.SaveChanges(); //保存数据操作 return RedirectToAction("List", "Role"); //跳转 }
3.实现效果,对角色定义后,他会自动跳到角色名列表,同时左边的菜单发生变更。
菜单效果下面再讲