/// <summary> /// 添加角色 /// </summary> /// <returns></returns> public ActionResult AddRole() { var allrights = from r in CQGJ.Right select r; //添加角色信息 if (Request.HttpMethod == "POST") { Role role = new Role(); role.RoleName = GetString("rolename"); CQGJ.AddToRole(role); List<string> keys = Request.Form.AllKeys.ToList(); keys.Remove("rolename"); //后处理插入 foreach (string k in keys) { RightsofRoles rr = new RightsofRoles(); rr.RoleID = role.RoleID; rr.RightID = Convert.ToInt16(k); CQGJ.AddToRightsofRoles(rr); CQGJ.SaveChanges(); } return RedirectToAction("rolemanager"); } return View(allrights); }
/// <summary> /// 修改角色 /// </summary> /// <returns></returns> public ActionResult EditRole(int id) { var role = (from r in CQGJ.Role where r.RoleID == id select r).First(); var allrights = from r in CQGJ.Right select r; var myrights = from ri in CQGJ.Right from rr in CQGJ.RightsofRoles where rr.RoleID == id && rr.RightID == ri.RightID select ri; //更新角色信息 if (Request.HttpMethod == "POST") { string[] keys = Request.Form.AllKeys; //先处理删除 foreach (var r in myrights) { if (!keys.Any(name => String.Equals(name, r.RightID.ToString()))) { int temp = r.RightID; RightsofRoles rightrole = (from rr in CQGJ.RightsofRoles where rr.RoleID == id && rr.RightID == temp select rr).First(); CQGJ.DeleteObject(rightrole); } } CQGJ.SaveChanges(); //后处理插入 foreach (string k in keys) { int temp = Convert.ToInt16(k); var recored = from rr in CQGJ.RightsofRoles where rr.RoleID == id && rr.RightID == temp select rr; if (recored.Count() <= 0)//小于0则插入新纪录 { RightsofRoles rr = new RightsofRoles(); rr.RoleID = id; rr.RightID = temp; CQGJ.AddToRightsofRoles(rr); CQGJ.SaveChanges(); } } myrights = from ri in CQGJ.Right from rr in CQGJ.RightsofRoles where rr.RoleID == id && rr.RightID == ri.RightID select ri; } ViewData["MyRole"] = role; ViewData["MyRights"] = myrights; return View(allrights); }