/// <summary> /// 添加和编辑的显示页面 /// </summary> /// <returns></returns> public ActionResult Edit(string id) { if (!string.IsNullOrEmpty(id) && id.Length == 36) { //show 编辑 ViewBag.ShowModel = service.GetModel(id); } else { ViewBag.ShowModel = new Base_RolesInfo(); } #region 给角色分配菜单 Base_MenuInfoService base_MenuInfoService = new Base_MenuInfoService(); var tmpRes = base_MenuInfoService.LoadEntities(m => m.DelFlag == 0).OrderBy(m => m.OrderBy).Select(m => new { m.Id, m.MenuName, m.ParentId }); StringBuilder builder = new StringBuilder(); if (tmpRes != null) { #region 找指定角色的所有已经选中的菜单menuIds Base_RoleMenuInfoService base_RoleMenuInfoService = new Base_RoleMenuInfoService(); var menuIds = base_RoleMenuInfoService.LoadEntities(rm => rm.RoleId == id).Select(rm => rm.MenuId); #endregion #region 默认情况下,一个角色对应多条菜单的赋值 StringBuilder builderDefaultMenus = new StringBuilder(); foreach (var item in menuIds) { builderDefaultMenus.Append(item + ","); } var dfStr = builderDefaultMenus.ToString(); if (dfStr.Length > 0) { ViewBag.DefaultMenus = dfStr.Remove(dfStr.Length - 1, 1); } #endregion foreach (var item in tmpRes) { if (menuIds != null && menuIds.Contains(item.Id)) { //选中已经选中的菜单 // { id: 'd', pId: 'w', name: 'd',checked:true }, builder.AppendLine("{id: '" + item.Id + "', pId: '" + item.ParentId + "', name: '" + item.MenuName + "',checked:true }, "); } else { builder.AppendLine("{id: '" + item.Id + "', pId: '" + item.ParentId + "', name: '" + item.MenuName + "' }, "); } } } ViewBag.SelectMenuInfo = builder.ToString(); #endregion return(View()); }