public PartialViewResult EditRolePower(int roleID) { List <RoleEditPgPowers> reppsli = new List <RoleEditPgPowers>(); foreach (uPages uPage in db.uPages.ToList()) { IEnumerable <uPowers> uPowers = db.uPowers.Where(n => n.pageID == uPage.pageID).ToList(); List <RoleEditPower> repli = new List <RoleEditPower>(); foreach (uPowers ups in uPowers) { bool hps = false; if (db.uRolePowers.Where(n => n.uRoleID == roleID && n.powerID == ups.powerID).FirstOrDefault() != null) { hps = true; } RoleEditPower rep = new RoleEditPower { pageID = ups.pageID, powerID = ups.powerID, powerDescription = ups.powerDescription, powerName = ups.powerName, hasPower = hps }; repli.Add(rep); } RoleEditPgPowers ppvmItem = new RoleEditPgPowers { uPages = uPage, roleEditPowers = repli }; reppsli.Add(ppvmItem); } RoleEditPowersViewModel repvm = new RoleEditPowersViewModel { roleID = roleID, roleEditPgPowers = reppsli }; return(PartialView(repvm)); }
public PartialViewResult EditRolePower(FormCollection form) { //if (!ModelState.IsValid) //{ // return PartialView(model); //} int roleID = Convert.ToInt32(form["roleID"]); List <int> powers = new List <int>(); foreach (var key in form.AllKeys) { if (key.Contains("powerID")) { powers.Add(Convert.ToInt32(form.Get(key))); } } IEnumerable <uRolePowers> roleurps = db.uRolePowers.Where(n => n.uRoleID == roleID).ToList(); foreach (uRolePowers urp in roleurps) { if (powers.Contains(urp.powerID)) { powers.Remove(urp.powerID); } else { db.uRolePowers.Remove(urp); } } foreach (int powerID in powers) { uRolePowers urps = new uRolePowers { uRoleID = roleID, powerID = powerID }; db.uRolePowers.Add(urps); } db.SaveChanges(); ViewBag.StatusMessage = "編輯權限成功。"; List <RoleEditPgPowers> reppsli = new List <RoleEditPgPowers>(); foreach (uPages uPage in db.uPages.ToList()) { IEnumerable <uPowers> uPowers = db.uPowers.Where(n => n.pageID == uPage.pageID).ToList(); List <RoleEditPower> repli = new List <RoleEditPower>(); foreach (uPowers ups in uPowers) { bool hps = false; if (db.uRolePowers.Where(n => n.uRoleID == roleID && n.powerID == ups.powerID).FirstOrDefault() != null) { hps = true; } RoleEditPower rep = new RoleEditPower { pageID = ups.pageID, powerID = ups.powerID, powerDescription = ups.powerDescription, powerName = ups.powerName, hasPower = hps }; repli.Add(rep); } RoleEditPgPowers ppvmItem = new RoleEditPgPowers { uPages = uPage, roleEditPowers = repli }; reppsli.Add(ppvmItem); } RoleEditPowersViewModel repvm = new RoleEditPowersViewModel { roleID = roleID, roleEditPgPowers = reppsli }; return(PartialView(repvm)); }