public ActionResult Create(RoleModel model) { var permissions = GetGroupForeignData(); ViewBag.Data_Permissions = permissions; if (ModelState.IsValid) { try { Roles rls = new Roles(); rls.Name = model.Name; rls.Description = model.Description; var permissionsArray = model.Permissions.Split(',').Select(x => Convert.ToInt32(x)).ToList(); var PermissionList = DB_Service.Set<Permissions>().Where(x => permissionsArray.Contains(x.ID)); rls.Permissions.AddRange(PermissionList); DB_Service.Add<Roles>(rls); DB_Service.Commit(); return RedirectToAction("index"); } catch (Exception ex) { ViewBag.Message = ex.Message; return View(model); } } else { return View(model); } }
public ActionResult Edit(int id) { RoleModel rml = new RoleModel(); Roles rs = new Roles(); rs = DB_Service.Set<Roles>().Include(x => x.Permissions).Single(x => x.ID == id); rml.ID = rs.ID; rml.Name = rs.Name; rml.Description = rs.Description; List<int> PermissionsList = new List<int>(); PermissionsList = rs.Permissions.Select(x => x.ID).ToList(); var permissions = GetGroupForeignData(PermissionsList); ViewBag.Data_Permissions = permissions; return View(rml); }
public ActionResult Edit(RoleModel model) { var permissionsArray = model.Permissions.Split(',').Select(x => Convert.ToInt32(x)).ToList(); var permissions = GetGroupForeignData(permissionsArray); ViewBag.Data_Permissions = permissions; if (ModelState.IsValid) { try { Roles rls = DB_Service.Set<Roles>().Include(x => x.Permissions).Single(x => x.ID == model.ID); DB_Service.Attach<Roles>(rls); rls.Name = model.Name; rls.Description = model.Description; var PermissionList = DB_Service.Set<Permissions>().Where(x => permissionsArray.Contains(x.ID)); var currentPermissionArray = rls.Permissions.Select(x => x.ID).ToList(); foreach (Permissions ps in DB_Service.Set<Permissions>()) { if (permissionsArray.Contains(ps.ID)) { if (!currentPermissionArray.Contains(ps.ID)) { rls.Permissions.Add(ps); } } else { if (currentPermissionArray.Contains(ps.ID)) { rls.Permissions.Remove(ps); } } } DB_Service.Commit(); return RedirectToAction("index"); } catch (Exception ex) { ViewBag.Message = ex.Message; return View(model); } } else { return View(model); } }