ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)(6)


public class RoleController : Controller
   {
       RoleRepository roleryrepository = new RoleRepository();
       MenuRepository menurepository = new MenuRepository();
       RoleMenuMappingRepository rmmrepositoy = new RoleMenuMappingRepository();
       #region 列表
       /// <summary>
       /// 默认页面
       /// </summary>
       /// <returns></returns>
       ///
       [Authorize]
       public ActionResult Index(int? page)
       {
           ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
           var source = roleryrepository.GetModelList();
           return View(roleryrepository.GetPageModelList(source, 10, page ?? 1));
       }
       #endregion
       #region 添加
       /// <summary>
       /// 添加页
       /// </summary>
       /// <param>实体类</param>
       /// <returns></returns>
       [Authorize]
       public ActionResult Add(Role model)
       {
           ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
           var menu = menurepository.GetModelListByState(1);
           ViewBag.Menu = new SelectList(menu, "MenuID", "MenuName");
           // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
           return View();
       }
       public string Log()
       {
           string meg = "";
           //int user = int.Parse(CookieHelper.GetValue("SysUserID"));
           //string ip = IPHelper.GetUserIp;
           foreach (var item in RouteData.Values)
           {
               meg += item.Key.ToUpper() + ":" + item.Value + "|";
           }
           return meg;
       }
       /// <summary>
       /// 添加事件
       /// </summary>
       /// <param>实体类</param>
       /// <param></param>
       /// <returns></returns>
       [Authorize, HttpPost, ValidateInput(false)]
       public ActionResult Add(Role model, FormCollection fc, int[] Menu)
       {
           try
           {
               ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
               model.State = 1;
               model.CreateTime = DateTime.Now;
               roleryrepository.SaveOrEditModel(model);
               if (Menu.Length > 0)
               {
                   foreach (int gsi in Menu)
                   {
                       RoleMenuMapping rmm = new RoleMenuMapping();
                       rmm.MenuID = gsi;
                       rmm.RoleID = model.RoleID;
                       rmmrepositoy.SaveOrEditModel(rmm);
                   }
               }
               else
               {
                   return RedirectToAction("Index");
               }
           }
           catch (Exception ex)
           {
               Log4netHelper.InvokeErrorLog(MethodBase.GetCurrentMethod().DeclaringType, ViewBag.ControllerName + "添加方法出错-Author:ryanding IP地址[" + IPHelper.GetUserIp + "]" + "操作人ID:" + CookieHelper.GetValue("SystemUserID") + "当前来源信息:" + Log(), ex);
           }
           return View("404");
       }
       #endregion
       #region 删除
       /// <summary>
       /// 删除
       /// </summary>
       /// <param>删除编号</param>
       /// <returns></returns>
       [Authorize]
       public ActionResult Delete(int id)
       {
           ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
           var model = roleryrepository.GetModel(id);
           model.State = 0;
           if (model != null)
           {
               roleryrepository.SaveOrEditModel(model);
               return RedirectToAction("index");
           }
           else
               return View("404");
       }
       #endregion
       #region 查询
       [Authorize]
       public ActionResult Search(int? page)
       {
           ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
           string name = "";
           if (page == null)
           {
               name = Request.Form["KeyWord"];
               CookieHelper.SetObj("KeyWord", 0, name);
           }
           else
               name = CookieHelper.GetValue("KeyWord");
           var query = QueryBuilder.Create<Role>().Like(d => d.RoleName, name);
           var source = roleryrepository.GetModelList().Where(query.Expression);
           return View(new Pager<Role>(source, page ?? 1, 10));
       }
       #endregion
       #region 编辑
       [Authorize]
       public ActionResult Edit(int id)
       {
           ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
           var rmmList = rmmrepositoy.GetModelList().Where(d => d.RoleID == id);
           var menu = menurepository.GetModelListByState(1);
           ViewBag.Menu = new SelectList(menu, "MenuID", "MenuName", string.Join(",", rmmList.Select(p => p.MenuID)));
           var model = roleryrepository.GetModel(id);
           if (model != null)
           {
               ViewData["State"] = SelectListHelper.GetStateSelectList(model.State);
               return View(model);
           }
           else
               return View("404");
       }
       [Authorize, AcceptVerbs(HttpVerbs.Post), ValidateInput(false)]
       public ActionResult Edit(int id, FormCollection fc, int[] Menu)
       {
           ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
           var model = roleryrepository.GetModel(id);
           if (model != null)
           {
               if (Menu.Length > 0)
               {
                   rmmrepositoy.DeleteModel(rmmrepositoy.GetModelList().Where(d => d.RoleID == model.RoleID));
                   foreach (int gsi in Menu)
                   {
                       RoleMenuMapping rmm = new RoleMenuMapping();
                       rmm.MenuID = gsi;
                       rmm.RoleID = model.RoleID;
                       rmmrepositoy.SaveOrEditModel(rmm);
                   }
               }
               UpdateModel(model);
               roleryrepository.SaveOrEditModel(model);
               return RedirectToAction("index");
           }
           else
               return View("404");
       }
       #endregion
   }

View代码

Index.cshtml

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzwdj.html