Example #1
0
 public ActionResult Create(Rol rol)
 {
     if (!string.IsNullOrEmpty(rol.nombre))
     {
         rol.nombre = rol.nombre.Replace("  ", " ").Trim();
         if (0 < db.Rol.Where(c => c.nombre.Equals(rol.nombre)).Count())
             ModelState.AddModelError("nombre", "El nombre del rol debe ser único");
     }
     bool hay = false;
     foreach (Opcion opcion in rol.lopcion)
         if (opcion.acceso)
         {
             hay = true;
             break;
         }
     if (!hay)
         ModelState.AddModelError("idrol", "Debe seleccionar al menos una opción");
     if (ModelState.IsValid)
     {
         db.Rol.Add(rol);
         db.SaveChanges();
         int? idsuperior = 0;
         foreach (Opcion opcion in rol.lopcion)
             if (opcion.acceso)
             {
                 if (opcion.idsuperior != idsuperior)
                 {
                     db.RolOpcion.Add(new RolOpcion() { idrol = rol.idrol, idopcion = (int)opcion.idsuperior, idsuperior = null });
                     db.SaveChanges();
                 }
                 db.RolOpcion.Add(new RolOpcion() { idrol = rol.idrol, idopcion = opcion.idopcion, idsuperior = opcion.idsuperior });
                 db.SaveChanges();
                 idsuperior = opcion.idsuperior;
             }
         TempData["alerta"] = "Rol registrado satisfactoriamente";
         return RedirectToAction("Index");
     }
     return View(rol);
 }
Example #2
0
 public ActionResult Create()
 {
     Rol rol = new Rol();
     rol.lopcion = db.Opcion.Where(x => x.idsuperior != null && !x.dinamico).OrderBy(x => x.opcionsuperior.link).ThenBy(x => x.link).ToList();
     return View(rol);
 }