Exemplo n.º 1
0
        public ActionResult Create(GroupModel model)
        {
            var roles = GetForeignData();
            ViewBag.Data_Roles = roles;

            if (ModelState.IsValid)
            {
                try
                {
                    Group gps = new Group();
                    gps.Name = model.Name;
                    gps.Description = model.Description;
                    var rolesArray = model.Roles.Split(',').Select(x => Convert.ToInt32(x));
                    var RoleList = DB_Service.Set<Roles>().Where(x => rolesArray.Contains(x.ID));
                    gps.Roles.AddRange(RoleList);
                    DB_Service.Add<Group>(gps);
                    DB_Service.Commit();
                    return RedirectToAction("index");
                }
                catch (Exception ex)
                {
                    ViewBag.Message = ex.Message;
                    return View(model);
                }
            }
            else
            {
                return View(model);
            }
        }
Exemplo n.º 2
0
 public ActionResult Edit(int id)
 {
     GroupModel gpm = new GroupModel();
     Group gps = new Group();
     gps = DB_Service.Set<Group>().Include(x => x.Roles).Single(x => x.GroupID == id);
     gpm.ID = gps.GroupID;
     gpm.Name = gps.Name;
     gpm.Description = gps.Description;
     List<int> RolesList = new List<int>();
     RolesList = gps.Roles.Select(x => x.ID).ToList();
     var roles = GetForeignData(RolesList);
     ViewBag.Data_RolesList = roles;
     return View(gpm);
 }
Exemplo n.º 3
0
        public ActionResult Edit(GroupModel model)
        {
            var rolesArray = model.Roles.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            var roles = GetForeignData(rolesArray);
            ViewBag.Data_RolesList = roles;
            if (ModelState.IsValid)
            {
                try
                {
                    Group gps = DB_Service.Set<Group>().Include(x => x.Roles).Single(x => x.GroupID == model.ID);
                    DB_Service.Attach<Group>(gps);
                    gps.Name = model.Name;
                    gps.Description = model.Description;
                    var RoleList = DB_Service.Set<Roles>().Where(x => rolesArray.Contains(x.ID));
                    var currentroleArray = gps.Roles.Select(x => x.ID).ToList();
                    foreach (Roles rl in DB_Service.Set<Roles>())
                    {
                        if (rolesArray.Contains(rl.ID))
                        {
                            if (!currentroleArray.Contains(rl.ID))
                            {
                                gps.Roles.Add(rl);
                            }
                        }
                        else
                        {
                            if (currentroleArray.Contains(rl.ID))
                            {
                                gps.Roles.Remove(rl);
                            }
                        }
                    }

                    DB_Service.Commit();
                    return RedirectToAction("index");
                }
                catch (Exception ex)
                {
                    ViewBag.Message = ex.Message;
                    return View(model);
                }
            }
            else
            {
                return View(model);
            }
        }