/// <summary> /// 更新角色的相关权限 /// </summary> /// <returns></returns> public JsonResult SetRoleRight2(RoleRightList data) { try { if (data == null) { throw new Exception("提供的更新列表不存在。"); } UserDbContext udb = new UserDbContext("ConnStr1"); List <RoleRight> list = udb.GetRoleRights(data.RoleID); //如果旧的在新的不存在,则删除 foreach (RoleRight rr in list) { bool flag = false; int i = 0; for (i = 0; i < data.RightIDList.Count; i++) { if (rr.RightID == data.RightIDList[i]) { flag = true; break; } } //如果不存在,则删除 if (flag == false) { udb.DeleteRoleRight(rr); } } //保存数据库变更 udb.SaveChanges(); //如果新的在旧的不存在,则新增 foreach (string str in data.RightIDList) { bool flag = false; int i = 0; for (i = 0; i < list.Count; i++) { if (str == list[i].RightID) { flag = true; break; } } //如果不存在,则新增 if (flag == false) { RoleRight rr = new RoleRight(); rr.RoleID = data.RoleID; rr.RightID = str; udb.AddRoleRight(rr); } } //保存数据库变更 udb.SaveChanges(); return(Json(data, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { if (ex.Message != null) { } throw; } }