public ActionResult Create(GroupModel model, FormCollection collection)
 {
     try
     {
         if (ModelState.IsValid)
         {
             Repository.Add(model);
             Repository.Save();
             return RedirectToAction("Index");
         }
         return View(model);
     }
     catch
     {
         return View();
     }
 }
 public void Add(GroupModel model)
 {
     Group group = new Group()
     {
         Name = model.Name,
         DefaultFormId = model.DefaultFormId,
         Role = model.Role
     };
     if (model.SelectedForms != null)
     {
         foreach (int id in model.SelectedForms)
         {
             Form form = db.Forms.Single(a => a.FormId == id);
             group.Forms.Add(form);
         }
     }
     db.Groups.Add(group);
 }
 public void Update(GroupModel model)
 {
     Group group = db.Groups.Single(d => d.GroupId == model.GroupId);
     group.Name = model.Name;
     group.DefaultFormId = model.DefaultFormId;
     group.Role = model.Role;
     foreach(Form form in group.Forms)
     {
         if(!model.SelectedForms.Contains(form.FormId))
         {
             foreach(Permission permission in db.Permissions.Where(m=>m.View.GroupId == model.GroupId && m.Field.FormId==form.FormId))
             {
                 db.Permissions.Remove(permission);
             }
         }
     }
     group.Forms.Clear();
     if (model.SelectedForms != null)
     {
         foreach (int id in model.SelectedForms)
         {
             Form form = db.Forms.Single(a => a.FormId == id);
             group.Forms.Add(form);
         }
     }
     db.SaveChanges();
 }
 public ActionResult Edit(GroupModel model)
 {
     try
     {
         SelectList roles = new SelectList(System.Web.Security.Roles.GetAllRoles());
         ViewBag.Roles = roles;
         Repository.Update(model);
         return RedirectToAction("Index");
     }
     catch
     {
         return View();
     }
 }