Ejemplo n.º 1
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Nombre,Descripcion")] Role role, string[] SelectedValues)
        {
            if (ModelState.IsValid)
            {
                using (var context = new DbContextUsers())
                {
                    var rolD = context.Roles.Include(q => q.Permissions).SingleOrDefault(q => q.Id == role.Id);
                    rolD.Permissions.Clear();
                    context.SaveChanges();
                    var rol      = context.Roles.SingleOrDefault(q => q.Id == role.Id);
                    var permisos = context.Permisos.Where(q => SelectedValues.Any(a => a == q.Id.ToString())).ToList();
                    rol.Permissions = permisos;
                    context.SaveChanges();
                }

                db.Entry(role).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(role));
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Name,Lastname,Phone,DateBirth,Password,Email")] User user, string[] SelectedRoles)
        {
            if (ModelState.IsValid)
            {
                using (var context = new DbContextUsers())
                {
                    var users = context.Users.Include(q => q.Roles).SingleOrDefault(q => q.Id == user.Id);
                    users.Roles.Clear();
                    context.SaveChanges();
                    var usuario = context.Users.SingleOrDefault(q => q.Id == user.Id);
                    var roles   = context.Roles.Where(q => SelectedRoles.Any(a => a == q.Id.ToString())).ToList();
                    usuario.Roles = roles;
                    context.SaveChanges();
                }

                db.Entry(user).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(user));
        }