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

系统角色篇

数据结构

ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)_第1张图片

用户管理

ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)_第2张图片

Controller代码

复制代码 代码如下:

public class SystemUserController : Controller
    {
        //public void 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 + "|";
        //    }
        //    AuditLogConfig.InsertAuditLog(meg, user, ip);
        //}
        SystemUserRepository sysuserrepository = new SystemUserRepository();
        RoleRepository roleryrepository = new RoleRepository();
        DepartmentRepository departmentryrepository = new DepartmentRepository();
        JobRepository jobryrepository = new JobRepository();
        #region 列表页
        ///
        /// 列表页
        ///

        /// 当前页
        ///
        [Authorize]
        public ActionResult Index(int? page)
        {
            ViewBag.ControllerName=RouteData.Values["controller"].ToString().ToLower();
            CookieHelper.Del("LogName");
            var source = sysuserrepository.GetPageModelList(10, page ?? 1, -1);
            return View(source);
        }
        #endregion
        #region 添加管理员
        ///
        /// 添加页
        ///

        /// 管理员实体类
        ///
        [Authorize]
        public ActionResult Add(SystemUser model)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var rolelist = roleryrepository.GetModelListByState(1);
            ViewBag.RoleID = SelectListExtension.ToSelectList(rolelist, d => d.RoleName, d => d.RoleID.ToString());
            var departmentlist = departmentryrepository.GetModelListByState(1);
            ViewBag.DepartmentID = SelectListExtension.ToSelectList(departmentlist, d => d.DepartmentName, d => d.DepartmentID.ToString());
            var joblist = jobryrepository.GetModelListByState(1);
            ViewBag.JobID = SelectListExtension.ToSelectList(joblist, d => d.JobName, d => d.JobID.ToString());
            // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
            return View();
        }
        ///
        /// 添加事件
        ///

        /// 实体类
        ///
        ///
        [Authorize, HttpPost, ValidateInput(false)]
        public ActionResult Add(SystemUser model, FormCollection fc)
        {
            model.State = 1;
            model.CreateTime = DateTime.Now;
            sysuserrepository.SaveOrEditModel(model);
            return RedirectToAction("Index");
        }
        #endregion
        #region 删除
        ///
        /// 删除
        ///

        /// 删除编号
        ///
        [Authorize]
        public ActionResult Delete(int id)
        {
            try
            {
                var model = sysuserrepository.GetModel(id);
                if (model != null)
                {
                    sysuserrepository.DeleteModel(id);
                    return RedirectToAction("Index");
                }
                else
                    return View("404");
                //throw new Exception("方法出错!");
            }
            catch (Exception ex)
            {
                Log4netHelper.InvokeErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "删除方法出错-Author:ryanding IP地址[" + IPHelper.GetUserIp + "]" + "操作人ID:" + CookieHelper.GetValue("SystemUserID"), ex);
                return View("404");
            }
        }
        #endregion
        #region 查询
        [Authorize]
        public ActionResult Search(int? page)
        {
            string logname = "";
            if (page == null)
            {
                logname = Request.Form["KeyWord"];
                CookieHelper.SetObj("KeyWord", 0, logname);
            }
            else
                logname = CookieHelper.GetValue("KeyWord");
            var query = QueryBuilder.Create().Like(d => d.LogName, logname);
            var source = sysuserrepository.GetModelList().Where(query.Expression);
            return View(new Pager(source, page ?? 1));
        }
        #endregion
        #region 编辑
        [Authorize]
        public ActionResult Edit(int id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = sysuserrepository.GetModel(id);
            if (model != null)
            {
                var rolelist = roleryrepository.GetModelListByState(1);
                ViewBag.RoleID = SelectListExtension.ToSelectList(rolelist, d => d.RoleName, d => d.RoleID.ToString(),model.RoleID);
                var departmentlist = departmentryrepository.GetModelListByState(1);
                ViewBag.DepartmentID = SelectListExtension.ToSelectList(departmentlist, d => d.DepartmentName, d => d.DepartmentID.ToString(), model.DepartmentID);
                var joblist = jobryrepository.GetModelListByState(1);
                ViewBag.JobID = SelectListExtension.ToSelectList(joblist, d => d.JobName, d => d.JobID.ToString(), model.JobID);
                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)
        {
            var model = sysuserrepository.GetModel(id);
            if (model != null)
            {
                string password = model.Password;
                if (Request.Form["PassWord"] != "")
                {
                    UpdateModel(model);
                    sysuserrepository.SaveOrEditModel(model);
                }
                else
                {
                    UpdateModel(model);
                    model.Password = password;
                    sysuserrepository.SaveOrEditModel(model);
                }
                return RedirectToAction("index");
            }
            else
                return View("404");
        }
        #endregion
        #region 用户名是否可用
        //检测用户名是否可用
        [HttpPost]
        public ActionResult GetUsers(string param, string name)
        {
            if (sysuserrepository.IsExist(param))
                return Content("y");
            else
                return Content("用户名已存在");
        }
        #endregion
        public bool Login()
        {
            try
            {
                //TODO:
                throw new Exception("方法出错!");
            }
            catch (Exception ex)
            {
                Log4netHelper.InvokeErrorLog(MethodBase.GetCurrentMethod().DeclaringType, "登录方法Login出错-Author:ryanding IP地址[" + IPHelper.GetUserIp + "]" + "操作人ID:" + CookieHelper.GetValue("SystemUserID"), ex);
                return false;
            }
            return true;
        }
    }

View代码

Index.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

           

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                    添加用户
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                       
                   
               
用户编号 登录名 手机 Email 昵称 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                           
                       
                   
@item.SystemUserID @(item.LogName) @item.Mobile @item.Email @item.TrueName @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

       

            @UIHelper.PagesHelper(ViewBag.ControllerName, Model.TotalPages, Model.PageIndex, Model.PageSize, Model.TotalCount, Model.HasPreviousPage, Model.HasNextPage, Model.PageList)
       

   



Add.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

   

       

           

添加用户


       

       

            @using (Html.BeginForm())
            {
               

                   
用户名:

                   

                       
                   

               

               

                   
昵称:

                   

                       
                   

               

               

                   
Email:

                   

                       
                   

               

               

                   
手机号:

                   

                       
                   

               

               

                   
密码:

                   

                       
                   

               

               

                   
确认密码:

                   

                       
                   

               

               

                   
岗位:

                   

                        @Html.DropDownList("JobID", ViewBag.JobID as SelectList)
                   

               

               

                   
部门:

                   

                         @Html.DropDownList("DepartmentID", ViewBag.DepartmentID as SelectList)
                   

               

               

                   
角色:

                   

                         @Html.DropDownList("RoleID", ViewBag.RoleID as SelectList)
                   

               

               

                   

                       
                   

               

            }
       

   



Edit.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Models.SystemUser

   

       

           

编辑用户


       

       

            @using (Html.BeginForm())
            {
               

                   
用户名:

                   

                       
                   

               

               

                   
昵称:

                   

                       
                   

               

               

                   
Email:

                   

                       
                   

               

               

                   
手机号:

                   

                       
                   

               

               

                   
修改密码:

                   

                       
                   

               

               

                   
岗位:

                   

                        @Html.DropDownList("JobID", ViewBag.JobID as SelectList)
                   

               

               

                   
部门:

                   

                         @Html.DropDownList("DepartmentID", ViewBag.DepartmentID as SelectList)
                   

               

               

                   
角色:

                   

                         @Html.DropDownList("RoleID", ViewBag.RoleID as SelectList)
                   

               

               

                   

                       
                   

               

            }
       

   



Search.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

           

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                    添加用户
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                       
                   
               
用户编号 登录名 手机 Email 昵称 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                           
                       
                   
@item.SystemUserID @(item.LogName) @item.Mobile @item.Email @item.TrueName @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

   



 角色管理

ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)_第3张图片

Controller代码

复制代码 代码如下:

public class RoleController : Controller
   {
       RoleRepository roleryrepository = new RoleRepository();
       MenuRepository menurepository = new MenuRepository();
       RoleMenuMappingRepository rmmrepositoy = new RoleMenuMappingRepository();
       #region 列表
       ///
       /// 默认页面
       ///

       ///
       ///
       [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 添加
       ///
       /// 添加页
       ///

       /// 实体类
       ///
       [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;
       }
       ///
       /// 添加事件
       ///

       /// 实体类
       ///
       ///
       [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 删除
       ///
       /// 删除
       ///

       /// 删除编号
       ///
       [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().Like(d => d.RoleName, name);
           var source = roleryrepository.GetModelList().Where(query.Expression);
           return View(new Pager(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

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

           

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                   添加
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                   
               
编号 角色名称 角色内容 创建时间 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                       
                   
@item.RoleID @item.RoleName @item.RoleContent @String.Format("{0:yyyy-MM-dd}", item.CreateTime) @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

       

           @UIHelper.PagesHelper(ViewBag.ControllerName, Model.TotalPages, Model.PageIndex, Model.PageSize, Model.TotalCount, Model.HasPreviousPage, Model.HasNextPage, Model.PageList);
       

   



Add.cshtml

复制代码 代码如下:

@using SJCRM.Helpers
@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

   

       

           

添加


       

       

            @using (Html.BeginForm())
            {
               

                   
角色名称:

                   

                       
                   

               

     

                   
角色权限:

                   

                        @Html.CheckBoxList("Menu")
                   

               

               

                   
角色内容:

                   

                        
                   

               

               

                   

                       
                   

               

            }
       

   



Edit.cshtml

复制代码 代码如下:

 @using SJCRM.Helpers
 @{
     ViewBag.Title = "Add";
     Layout = "~/Views/Shared/_Layout.cshtml";
 }
 @model SJCRM.Models.Role
 

    

        

            

编辑


        

        

             @using (Html.BeginForm())
             {
                

                    
角色名称:

                    

                        
                    

                

      

                    
角色权限:

                    

                         @Html.CheckBoxList("Menu")
                    

                

                

                    
角色内容:

                    

                         
                    

                

                

                    

                        
                    

                

             }
        

    

 

 

Search.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

           

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                   添加
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                   
               
编号 角色名称 角色内容 创建时间 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                       
                   
@item.RoleID @item.RoleName @item.RoleContent @String.Format("{0:yyyy-MM-dd}", item.CreateTime) @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

   



 菜单管理

ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)_第4张图片

Controller代码

复制代码 代码如下:

public class MenuController : Controller
    {
        MenuRepository repository = new MenuRepository();
        #region 列表
        ///
        /// 默认页面
        ///

        ///
        ///
        [Authorize]
        public ActionResult Index(int? page)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var source = repository.GetModelListByState(1);
            return View(repository.GetPageModelList(source, 10, page ?? 1));
        }
        #endregion
        #region 添加
        ///
        /// 添加页
        ///

        /// 实体类
        ///
        [Authorize]
        public ActionResult Add(Menu model)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
            return View();
        }
        ///
        /// 添加事件
        ///

        /// 实体类
        ///
        ///
        [Authorize, HttpPost, ValidateInput(false)]
        public ActionResult Add(Menu model, FormCollection fc)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            model.State = 1;
            model.CreateTime = DateTime.Now;
            repository.SaveOrEditModel(model);
            return RedirectToAction("Index");
        }
        #endregion
        #region 删除
        ///
        /// 删除
        ///

        /// 删除编号
        ///
        [Authorize]
        public ActionResult Delete(int id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = repository.GetModel(id);
            model.State = 0;
            if (model != null)
            {
                repository.SaveOrEditModel(model);
                return RedirectToAction("index");
            }
            else
                return View("404");
            //var model = categoryrepository.GetModel(id);
            //if (model != null)
            //{
            //    categoryrepository.DeleteModel(id);
            //    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().Like(d => d.MenuName, name);
            var source = repository.GetModelList().Where(query.Expression);
            return View(new Pager(source, page ?? 1, 10));
        }
        #endregion
        #region 编辑
        [Authorize]
        public ActionResult Edit(int id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = repository.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)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = repository.GetModel(id);
            if (model != null)
            {
                UpdateModel(model);
                repository.SaveOrEditModel(model);
                return RedirectToAction("index");
            }
            else
                return View("404");
        }
        #endregion
     }

View代码

Index.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

           

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                   添加
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                       
                   
               
编号 菜单名称 别名样式 连接地址 创建时间 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                           
                       
                   
@item.MenuID @item.MenuName @item.Alias @item.Url @String.Format("{0:yyyy-MM-dd}", item.CreateTime) @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

       

           @UIHelper.PagesHelper(ViewBag.ControllerName, Model.TotalPages, Model.PageIndex, Model.PageSize, Model.TotalCount, Model.HasPreviousPage, Model.HasNextPage, Model.PageList);
       

   



Add.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

   

       

           

添加菜单


       

       

            @using (Html.BeginForm())
            {
               

                   
菜单名称:

                   

                       
                   

               

               

                   
连接地址:

                   

                       
                   

               

               

                   
别名样式:

                   

                       
                   

               

               

                   

                       
                   

               

            }
       

   



Edit.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Models.Menu

   

       

           

编辑


       

       

            @using (Html.BeginForm())
            {
               

                   
菜单名称:

                   

                       
                   

               

               

                   
连接地址:

                   

                       
                   

               

               

                   
别名样式:

                   

                       
                   

               

               

                   

                       
                   

               

            }
       

   



Search.cshtml

复制代码 代码如下:

 @{
     ViewBag.Title = "Index";
     Layout = "~/Views/Shared/_Layout.cshtml";
 }
 @model SJCRM.Helpers.Pager
 

    

        

            

                

                    
关键词:

                    

                        

                    

                        

                

            

        

        

            

                

                    添加
                

            

            

                
                    
                        
                        
                        
                        
                        
                        
                        
                    
                
编号 菜单名称 别名样式 连接地址 创建时间 状态 操作

            

            

                 @{int classnum = 1;}
                 @foreach (var item in Model)
                 {
                                                                                                         {class="rgbg2"}>
                        
                            
                            
                            
                            
                            
                            
                            
                        
                    
@item.MenuID @item.MenuName @item.Alias @item.Url @String.Format("{0:yyyy-MM-dd}", item.CreateTime) @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                    classnum = classnum + 1;
                 }
            

        

    

 

 

 部门管理

ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二)_第5张图片

Controller代码

复制代码 代码如下:

 public class DepartmentController : Controller
    {
        DepartmentRepository departmentryrepository = new DepartmentRepository();
        #region 列表
        ///
        /// 默认页面
        ///

        ///
        ///
        [Authorize]
        public ActionResult Index(int? page)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var source = departmentryrepository.GetModelList();
            return View(departmentryrepository.GetPageModelList(source, 10, page ?? 1));
        }
        #endregion
        #region 添加
        ///
        /// 添加页
        ///

        /// 实体类
        ///
        [Authorize]
        public ActionResult Add(Department model)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
            return View();
        }
        ///
        /// 添加事件
        ///

        /// 实体类
        ///
        ///
        [Authorize, HttpPost, ValidateInput(false)]
        public ActionResult Add(Department model, FormCollection fc)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            model.State = 1;
            model.CreateTime = DateTime.Now;
            departmentryrepository.SaveOrEditModel(model);
            return RedirectToAction("Index");
        }
        #endregion
        #region 删除
        ///
        /// 删除
        ///

        /// 删除编号
        ///
        [Authorize]
        public ActionResult Delete(int id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = departmentryrepository.GetModel(id);
            model.State = 0;
            if (model != null)
            {
                departmentryrepository.SaveOrEditModel(model);
                return RedirectToAction("index");
            }
            else
                return View("404");
            //var model = categoryrepository.GetModel(id);
            //if (model != null)
            //{
            //    categoryrepository.DeleteModel(id);
            //    return RedirectToAction("Index");
            //}
            //else
            //    return View("404");
        }
        #endregion
        #region 查询
        [Authorize]
        public ActionResult Search(int? page)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            string jobname = "";
            if (page == null)
            {
                jobname = Request.Form["KeyWord"];
                CookieHelper.SetObj("KeyWord", 0, jobname);
            }
            else
                jobname = CookieHelper.GetValue("KeyWord");
            var query = QueryBuilder.Create().Like(d => d.DepartmentName, jobname);
            var source = departmentryrepository.GetModelList().Where(query.Expression);
            return View(new Pager(source, page ?? 1, 10));
        }
        #endregion
        #region 编辑
        [Authorize]
        public ActionResult Edit(int id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = departmentryrepository.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)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = departmentryrepository.GetModel(id);
            if (model != null)
            {
                UpdateModel(model);
                departmentryrepository.SaveOrEditModel(model);
                return RedirectToAction("index");
            }
            else
                return View("404");
        }
        #endregion
      }

View代码

Index.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

            

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                   添加
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                   
               
编号 部门名称 是否销售 创建时间 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                       
                   
@item.DepartmentID @item.DepartmentName @(item.IsSale == 1 ? "是" : "否") @String.Format("{0:yyyy-MM-dd}", item.CreateTime) @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

       

           @UIHelper.PagesHelper(ViewBag.ControllerName, Model.TotalPages, Model.PageIndex, Model.PageSize, Model.TotalCount, Model.HasPreviousPage, Model.HasNextPage, Model.PageList);
       

   



Add.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

   

       

           

添加


       

       

            @using (Html.BeginForm())
            {
               

                   
部门名称:

                   

                       
                   

               

               

                   
是否销售:

                   

                       
                   

               

               

                   

                       
                   

               

            }
       

   



Edit.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Models.Department

   

       

           

编辑


       

       

            @using (Html.BeginForm())
            {
               

                   
部门名称:

                   

                       
                   

               

               

                   
是否销售:

                   

                        @UIHelper.IsSelect("IsSale", @Model.IsSale.ToString());
                   

               

               

                   

                       
                   

               

            }
       

   



Search.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

            

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                   添加
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                   
               
编号 部门名称 是否销售 创建时间 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                       
                   
@item.DepartmentID @item.DepartmentName @(item.IsSale == 1 ? "是" : "否") @String.Format("{0:yyyy-MM-dd}", item.CreateTime) @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

   



 岗位管理

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

Controller代码

复制代码 代码如下:

 public class JobController : Controller
    {
        JobRepository jobryrepository = new JobRepository();
        #region 岗位列表
        ///
        /// 默认页面
        ///

        ///
        ///
        [Authorize]
        public ActionResult Index(int? page)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var source = jobryrepository.GetModelListByState(1);
            return View(jobryrepository.GetPageModelList(source, 10, page ?? 1));
        }
        #endregion
        #region 添加
        ///
        /// 添加页
        ///

        /// 实体类
        ///
        [Authorize]
        public ActionResult Add(Job model)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            // ViewData["State"] = SelectListHelper.GetIsOrNoSelectList(0);
            return View();
        }
        ///
        /// 添加事件
        ///

        /// 实体类
        ///
        ///
        [Authorize, HttpPost, ValidateInput(false)]
        public ActionResult Add(Job model, FormCollection fc)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            model.State = 1;
            model.CreateTime = DateTime.Now;
            jobryrepository.SaveOrEditModel(model);
            return RedirectToAction("Index");
        }
        #endregion
        #region 删除
        ///
        /// 删除
        ///

        /// 删除编号
        ///
        [Authorize]
        public ActionResult Delete(int id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = jobryrepository.GetModel(id);
            model.State = 0;
            if (model != null)
            {
                jobryrepository.SaveOrEditModel(model);
                return RedirectToAction("index");
            }
            else
                return View("404");
            //var model = categoryrepository.GetModel(id);
            //if (model != null)
            //{
            //    categoryrepository.DeleteModel(id);
            //    return RedirectToAction("Index");
            //}
            //else
            //    return View("404");
        }
        #endregion
        #region 查询
        [Authorize]
        public ActionResult Search(int? page)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            string jobname = "";
            if (page == null)
            {
                jobname = Request.Form["KeyWord"];
                CookieHelper.SetObj("KeyWord", 0, jobname);
            }
            else
                jobname = CookieHelper.GetValue("KeyWord");
            var query = QueryBuilder.Create().Like(d => d.JobName, jobname);
            var source = jobryrepository.GetModelList().Where(query.Expression);
            return View(new Pager(source, page ?? 1, 10));
        }
        #endregion
        #region 编辑
        [Authorize]
        public ActionResult Edit(int id)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = jobryrepository.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)
        {
            ViewBag.ControllerName = RouteData.Values["controller"].ToString().ToLower();
            var model = jobryrepository.GetModel(id);
            if (model != null)
            {
                UpdateModel(model);
                jobryrepository.SaveOrEditModel(model);
                return RedirectToAction("index");
            }
            else
                return View("404");
        }
        #endregion
    }

View代码

Index.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

            

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                    添加
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                   
               
编号 岗位名称 岗位说明 是否主管 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                       
                   
@item.JobID @item.JobName @item.JobContent @(item.IsManager == 1 ? "是" : "否") @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

       

            @UIHelper.PagesHelper(ViewBag.ControllerName, Model.TotalPages, Model.PageIndex, Model.PageSize, Model.TotalCount, Model.HasPreviousPage, Model.HasNextPage, Model.PageList);
       

   



Add.cshtml

复制代码 代码如下:

 @{
     ViewBag.Title = "Add";
     Layout = "~/Views/Shared/_Layout.cshtml";
 }
 

    

        

            

添加


        

        

             @using (Html.BeginForm())
             {
                

                    
岗位名称:

                    

                        
                    

                

                

                    
是否主管:

                    

                        
                    

                

                

                    
岗位说明:

                    

                         
                    

                

                

                    

                        
                    

                

             }
        

    

 

 

Edit.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Add";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Models.Job

   

       

           

编辑


       

       

            @using (Html.BeginForm())
            {
               

                   
岗位名称:

                   

                       
                   

               

               

                   
是否主管:

                   

                        @UIHelper.IsSelect("IsManager",Model.IsManager.ToString());
                   

               

               

                   
岗位说明:

                   

                       
                   

               

               

                   

                       
                   

               

            }
       

   



@*
*@

@**@

Search.cshtml

复制代码 代码如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@model SJCRM.Helpers.Pager

   

       

            

               

                   
关键词:

                   

                       

                   

                       

               

           

       

       

           

               

                    添加
               

           

           

               
                   
                       
                       
                       
                       
                       
                       
                   
               
编号 岗位名称 岗位说明 是否主管 状态 操作

           

           

                @{int classnum = 1;}
                @foreach (var item in Model)
                {
                                                                                                       {class="rgbg2"}>
                       
                           
                           
                           
                           
                           
                           
                       
                   
@item.JobID @item.JobName @item.JobContent @(item.IsManager == 1 ? "是" : "否") @(item.State == 1 ? "有效" : "无效") 删除 | 编辑 

                                                                                   classnum = classnum + 1;
                }
           

       

   



你可能感兴趣的:(ASP.NET.4.5.1+MVC5.0设置系统角色与权限(二))