24.把新增和编辑里的(父菜单选择:做成下拉框)
EditPermission.cshtml(新增和编辑)
//父菜单显示绑定
@model MODEL.ViewModel.Permission <script type="text/javascript" src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>@*//它不验证,只负责读属性*@ <style type="text/css"> #modifyTb { width: 400px; border: 1px solid #0094ff; margin: 10px auto; border-collapse: collapse; } #modifyTb td, #modifyTb th { border: 1px solid #0094ff; padding: 4px; } </style> @using(Html.BeginForm()){//BeginForm方法 如果没有通过参数 设置 提交的路径,那么就会直接拿当前访问的路径 当 action <td>@Html.HiddenFor(model => model.pid)</td> @*//权限id*@ <table id="modifyTb"> <tr> <td>父菜单</td> <td>@Html.Raw(ViewBag.parentMenuHtml)</td>//父菜单显示绑定 </tr> <tr> <td>@Html.LabelFor(model => model.pName)</td> <td>@Html.TextBoxFor(model => model.pName)</td> <td>@Html.ValidationMessageFor(model => model.pName)</td> </tr> <tr> <td>@Html.LabelFor(model => model.pAreaName)</td> <td>@Html.TextBoxFor(model => model.pAreaName)</td> <td>@Html.ValidationMessageFor(model => model.pAreaName)</td> </tr> <tr> <td>@Html.LabelFor(model => model.pControllerName)</td> <td>@Html.TextBoxFor(model => model.pControllerName)</td> <td>@Html.ValidationMessageFor(model => model.pControllerName)</td> </tr> <tr> <td>@Html.LabelFor(model => model.pActionName)</td> <td>@Html.TextBoxFor(model => model.pActionName)</td> <td>@Html.ValidationMessageFor(model => model.pActionName)</td> </tr> <tr> <td>@Html.LabelFor(model => model.pFormMethod)</td> <td>@Html.DropDownListFor(model => model.pFormMethod, ViewBag.httpMethopList as IEnumerable<SelectListItem>)</td> <td>@Html.ValidationMessageFor(model => model.pFormMethod)</td> </tr> <tr> <td>@Html.LabelFor(model => model.pOperationType)</td> <td>@Html.DropDownListFor(model => model.pOperationType, ViewBag.OperationTypeList as IEnumerable<SelectListItem>)</td> <td>@Html.ValidationMessageFor(model => model.pOperationType)</td> </tr> <tr> <td>@Html.LabelFor(model => model.pOrder)</td> <td>@Html.TextBoxFor(model => model.pOrder)</td> <td>@Html.ValidationMessageFor(model => model.pOrder)</td> </tr> <tr> <td>@Html.LabelFor(model => model.pIsShow)</td> <td>@Html.EditorFor(model => model.pIsShow)</td> <td>@Html.ValidationMessageFor(model => model.pIsShow)</td> </tr> <tr> <td>@Html.LabelFor(model => model.pRemark)</td> <td>@Html.TextBoxFor(model => model.pRemark)</td> </tr> <tr> <td colspan="2" style="text-align:center"><input type="submit" value="保存" /></td> </tr> </table>
控制器、
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Mvc; using MVCOA.Helper; using Common.Attributes; namespace MVCOA.Login.Admin { /// <summary> /// 系统管理 /// </summary> public class SysController : Controller { #region 1.0 权限列表 视图 +Permission() [HttpGet] /// <summary> /// 权限列表 视图 /// </summary> /// <returns></returns> public ActionResult Permission() { return View(); } #endregion #region 1.1 权限列表 数据 +GetPermData() [HttpPost] /// <summary> /// 权限列表 视图 /// </summary> /// <returns></returns> public ActionResult GetPermData() { SetDropDoneList(); //获取页容量 int pageSize = int.Parse(Request.Form["rows"]); //获取请求的页面 int pageIndex = int.Parse(Request.Form["page"]); //查询分页数据 var list = OperateContext.Current.BLLSession.IOu_PermissionBLL.GetPagedList(pageIndex, pageSize, p => p.pIsDel == false, p => p.pid).Select(p => p.ToPOCO()); //总行数 var rowCount = OperateContext.Current.BLLSession.IOu_PermissionBLL.GetListBy(p=>p.pParent==1 && p.pIsDel == false,p=>p.pid).Count();//没有删除的,菜单等级是1的 MODEL.EasyUIModel.DataGridModel dgModel = new MODEL.EasyUIModel.DataGridModel() { total = rowCount, rows = list, footer = null }; return Json(dgModel); } #endregion #region 1.2加载 权限修改 窗体html [HttpGet] /// <summary> /// 1.2加载 权限修改 窗体html /// </summary> /// <returns></returns> public ActionResult EditPermission(int id)//这个是留有 { ViewBag.parentMenuHtml = GetParentMenuDrow();//1.2.下拉放到viewBag.parentMenuHtml var listt = OperateContext.Current.BLLSession.IOu_PermissionBLL.GetListBy(c => c.pid == id).FirstOrDefault().ToViewModel(); //准备请求方式下拉框数据 SetDropDoneList(); return PartialView(listt); } #endregion #region 1.2 权限修改 +EditPermission(MODEL.ViewModel.Permission model) [HttpPost] [AjaxRequest] /// <summary> /// 1.2 权限修改 /// </summary> /// <returns></returns> public ActionResult EditPermission(MODEL.Ou_Permission model) { int res = OperateContext.Current.BLLSession.IOu_PermissionBLL.Modify(model, "pName", "pAreaName", "pControllerName", "pActionName", "pFormMethod", "pOperationType", "pOrder", "pIsShow", "pRemark"); if(res>0){ return Redirect("/admin/sys/Permission?ok"); } //} catch { else{ return Redirect("/admin/sys/Permission?err"); } } #endregion #region 1.3显示 新增权限表单代码 [HttpGet] [AjaxRequest] /// <summary> /// 显示新增表单 /// </summary> /// <returns></returns> public ActionResult AddPermission() { //准备请求方式下拉框数据 SetDropDoneList(); ViewBag.parentMenuHtml = GetParentMenuDrow();//1.1.下拉放到viewBag.parentMenuHtml return PartialView("EditPermission");//使用指定的视图创造呈现//partial局部的, } /// <summary> /// 1.生成 父菜单 下拉框 html字符串 /// </summary> /// <returns></returns> string GetParentMenuDrow() { //1.查询所有的父菜单 var list = OperateContext.Current.BLLSession.IOu_PermissionBLL.GetListBy(p => p.pParent == 1).Select(p => p.ToViewModel()).ToList(); System.Text.StringBuilder sb = new StringBuilder("<select name='pParent'><option value=1>父菜单</option>", 1000); list.ForEach(p => sb.AppendLine("<option value='" + p.pid + "'>" + p.pName + "</option>")); sb.AppendLine("</select>"); return sb.ToString(); } #endregion #region 1.3显示 新增权限表单代码 +EditPermission(MODEL.ViewModel.Permission model) [HttpPost] [AjaxRequest] /// <summary> /// 1.2 1.3显示 新增权限表单代码 /// </summary> /// <returns></returns> public ActionResult AddPermission(MODEL.Ou_Permission model) { model.pAddTime = DateTime.Now; model.pIsDel = false; int res = OperateContext.Current.BLLSession.IOu_PermissionBLL.Add(model); //下拉帮助类 SetDropDoneList(); if (res > 0) return Redirect("/admin/sys/Permission?ok"); else return Redirect("/admin/sys/Permission?err"); } #endregion #region 1.4删除权限操作+ActionResult DelPermission() /// <summary> /// 删除权限操作+DelPermission() /// </summary> /// <returns></returns> [HttpPost] [AjaxRequest] public ActionResult DelPermission() { try { int id = int.Parse(Request.Form["id"]);//获取要删除的id OperateContext.Current.BLLSession.IOu_PermissionBLL.DelBy(d => d.pid == id);//赋值给pid. return OperateContext.Current.RedirectAjax("ok", "删除成功了!!", null, ""); } catch { return OperateContext.Current.RedirectAjax("err", "您现在删除的权限正在被角色使用,请先取消角色中的这个权限!", null, ""); } } #endregion #region 1.2子权限列表 视图+PermissionSon() /// <summary> /// 子权限列表视图 /// </summary> /// <returns></returns> [HttpGet] [AjaxRequest] public ActionResult PermissionSon() { //获取父权限id int pid = int.Parse(Request.QueryString["pid"]); return View(); } #endregion /// <summary> /// 查询子权限列表数据 /// </summary> /// <returns></returns> [HttpPost] [AjaxRequest] public ActionResult PermissionSon(FormCollection form) { //获取页容量 int pageSize = int.Parse(Request.Form["rows"]); //获取请求的页面 int pageIndex = int.Parse(Request.Form["page"]); //获取父权限id int parentMenid=int.Parse(Request.QueryString["pid"]); //查询分页数据 var list = OperateContext.Current.BLLSession.IOu_PermissionBLL.GetPagedList(pageIndex, pageSize,p=>p.pParent==parentMenid && p.pIsDel == false,p=>p.pid).Select(p => p.ToPOCO()); int rowCount = OperateContext.Current.BLLSession.IOu_PermissionBLL.GetListBy(p => p.pParent==parentMenid && p.pIsDel==false).Count(); //2生成规定格式的 json字符串发回 给异步对象 MODEL.EasyUIModel.DataGridModel dgModel = new MODEL.EasyUIModel.DataGridModel() { total=rowCount, rows=list, footer=null }; return Json(dgModel); } /// <summary> /// 下拉帮助类 /// </summary> void SetDropDoneList() { //准备请求方式下拉框数据 ViewBag.httpMethopList = new List<SelectListItem>() { new SelectListItem(){Text="Get",Value="1"}, new SelectListItem(){Text="Post",Value="2"}, new SelectListItem(){Text="Both",Value="3"}//Both两个都可以 }; //操作方式 ViewBag.OperationTypeList = new List<SelectListItem>() { new SelectListItem(){Text="无操作",Value="0"}, new SelectListItem(){Text="eastyui连接",Value="1"}, new SelectListItem(){Text="打开新窗体",Value="2"} }; } } }