Ejemplo n.º 1
0
        public async Task<ActionResult> EditUser(string id, EditUserViewModel model)
        {
            var Db = new ApplicationDbContext();
            var list = Db.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
            model.Roles = (IEnumerable<SelectListItem>)list;

            if (ModelState.IsValid)
            {
                var user = Db.Users.First(u => u.Id == id);
                user.FirstName = model.user.FirstName;
                user.LastName = model.user.LastName;
                user.Email = model.user.Email;
                user.UserName = model.user.Email;
                user.Gender = model.user.Gender;
                user.PhoneNumber = model.user.PhoneNumber;
                user.City = model.user.City;
                user.State = model.user.State;
                user.Country = model.user.Country;
                user.Zip = model.user.Zip;
                user.AddressLine = model.user.AddressLine;
                user.Salary = model.user.Salary;
                Db.Entry(user).State = System.Data.Entity.EntityState.Modified;

                var Roles = UserManager.GetRoles(id);
                if(!Roles.First().Equals(model.Role))
                { 
                    await UserManager.AddToRoleAsync(user.Id, model.Role);
                    await UserManager.RemoveFromRoleAsync(user.Id, Roles.First());
                }

                await Db.SaveChangesAsync();
                return RedirectToAction("ListUsers");
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult EditUser(string id)
        {
            var Db = new ApplicationDbContext();
            var user = Db.Users.First(u => u.Id == id);
            if (user == null)
            {
                return HttpNotFound();
            }
            EditUserViewModel model = new EditUserViewModel();

            var userRoles = UserManager.GetRoles(user.Id);
            var list = Db.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem 
            {
                Selected = userRoles.Contains(rr.Name),
                Value = rr.Name.ToString(), 
                Text = rr.Name }).ToList();

            model.user = user;
            model.Roles = (IEnumerable<SelectListItem>)list;
            return View(model);
        }