public ActionResult Create() { var model = new AdmUserFormViewModel(); //ViewBag.RolesList = RoleManager.Roles; ViewBag.AdmProfileesList = ServiceAdmProfile.GetList(); return(View(model)); }
public ActionResult Edit(AdmUserFormViewModel model) { try { ViewBag.AdmProfileesList = ServiceAdmProfile.GetList(); if (ModelState.IsValid) { if (string.IsNullOrEmpty(model.AccountId)) { var user = UserManager.FindByEmail(model.Email); if (user == null) { user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = UserManager.Create(user); result = UserManager.SetLockoutEnabled(user.Id, false); if (result.Succeeded) { model.AccountId = user.Id; // Add user admin to Role Admin if not already added UserManager.AddToRole(user.Id, "Administrator"); } } else { model.AccountId = user.Id; } } var AdmUser = ServiceAdmUser.GetItem(model.UserId); AdmUser.Name = model.Name; AdmUser.LastName = model.LastName; AdmUser.Email = model.Email; AdmUser.AccountId = model.AccountId; //Update from Identity AdmUser.Active = model.Active; AdmUser.AdmProfileId = model.AdmProfileId; ServiceAdmUser.Edit(AdmUser); } else { ModelState.AddModelError("Error", "Algunos datos ingresados no son válidos"); return(View(model)); } } catch (Exception ex) { ModelState.AddModelError("Error", "Se ha producido un error: " + ex.Message); return(View(model)); } return(RedirectToAction("Index")); }
public ActionResult Create(AdmUserFormViewModel model) { try { ViewBag.AdmProfileesList = ServiceAdmProfile.GetList(); if (ModelState.IsValid) { var user = UserManager.FindByEmail(model.Email); if (user == null) { user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = UserManager.Create(user, model.Password); result = UserManager.SetLockoutEnabled(user.Id, false); if (result.Succeeded) { model.AccountId = user.Id; // Add user admin to Role Admin if not already added UserManager.AddToRole(user.Id, "Administrator"); } } else { model.AccountId = user.Id; } ServiceAdmUser.Add(new AdmUserDto() { Name = model.Name, LastName = model.LastName, Email = model.Email, AccountId = model.AccountId, //Update from Identity RegisterDate = DateTime.Now, Active = model.Active, AdmProfileId = model.AdmProfileId }); } else { ModelState.AddModelError("Error", "Algunos datos ingresados no son válidos"); return(View(model)); } } catch (Exception ex) { ModelState.AddModelError("Error", "Se ha producido un error: " + ex.Message); return(View(model)); } return(RedirectToAction("Index")); }