public ActionResult UserAddEditPartial(NorthOps.Ops.Models.User item) { var user = UserManager.FindById(item.Id); if (user != null) { user.userRole = user.UserRoles.FirstOrDefault() == null ? "" : user.UserRoles.FirstOrDefault().Name; } ViewBag.TownCity = new UnitOfWork().TownCityRepo.Get(); return(PartialView("_useraddeditpartial", user)); }
public async Task <ActionResult> UserGridViewPartialAddNew(NorthOps.Ops.Models.User item) { var model = new object[0]; if (ModelState.IsValid) { try { item.Id = Guid.NewGuid().ToString(); var res = await UserManager.CreateAsync(item, item.Password); if (res.Succeeded) { await UserManager.AddToRoleAsync(item.Id, item.userRole); unitOfWork.JobApplicationRepo.Insert(new JobApplication() { JobApplicationId = Guid.NewGuid(), UserId = item.Id }); await unitOfWork.SaveAsync(); } else { ViewData["model"] = item; ViewData["EditError"] = string.Join(Environment.NewLine, res.Errors); } } catch (Exception e) { ViewData["EditError"] = e.Message; } } else { StringBuilder stringBuilder = new StringBuilder(); foreach (var m in ModelState) { if (m.Value.Errors.Count() > 0) { ModelState.AddModelError(m.Key, string.Join(Environment.NewLine, m.Value.Errors.Select(x => x.ErrorMessage))); stringBuilder.AppendLine(m.Value.Errors[0].ErrorMessage); } } ViewData["EditError"] = "Please, correct all errors." + Environment.NewLine + stringBuilder.ToString(); ViewData["Model"] = item; } return(PartialView("_UserGridViewPartial", unitOfWork.UserRepository.Get(includeProperties: "UserRoles"))); }
public async Task <ActionResult> UserGridViewPartialDelete(NorthOps.Ops.Models.User item) { if (item.Id != null) { try { await UserManager.DeleteAsync(await UserManager.FindByIdAsync(item.Id)); } catch (Exception e) { ViewData["EditError"] = e.Message; } } return(PartialView("_UserGridViewPartial", unitOfWork.UserRepository.Get(includeProperties: "UserRoles"))); }
public async Task <ActionResult> UserGridViewPartialUpdate(NorthOps.Ops.Models.User item) { if (ModelState.IsValid) { try { var user = await UserManager.FindByIdAsync(item.Id); user.FirstName = item.FirstName; user.LastName = item.LastName; user.MiddleName = item.MiddleName; user.Gender = item.Gender; user.BirthDate = item.BirthDate; user.AddressLine1 = item.AddressLine1; user.AddressLine2 = item.AddressLine2; user.TownCity = item.TownCity; user.Cellular = item.Cellular; user.Religion = item.Religion; user.Citizenship = item.Citizenship; user.Languages = item.Languages; user.CivilStatus = item.CivilStatus; user.Skills = item.Skills; await UserManager.UpdateAsync(user); #region UpdateRole var roles = await UserManager.GetRolesAsync(user.Id); await UserManager.RemoveFromRolesAsync(user.Id, roles.ToArray()); await UserManager.AddToRoleAsync(user.Id, item.userRole); #endregion if (item.Password != null) { var token = await UserManager.GeneratePasswordResetTokenAsync(item.Id); var res = await UserManager.ResetPasswordAsync(item.Id, token, item.Password); #if DEBUG if (!res.Succeeded) { Debug.WriteLine(string.Join(",", res.Errors)); } #endif } } catch (Exception e) { ViewData["EditError"] = e.Message; } } else { StringBuilder stringBuilder = new StringBuilder(); foreach (var m in ModelState) { if (m.Value.Errors.Count() > 0) { ModelState.AddModelError(m.Key, string.Join(Environment.NewLine, m.Value.Errors.Select(x => x.ErrorMessage))); stringBuilder.AppendLine(m.Value.Errors[0].ErrorMessage); } } ViewData["EditError"] = "Please, correct all errors." + Environment.NewLine + stringBuilder.ToString(); ViewData["Model"] = item; } return(PartialView("_UserGridViewPartial", unitOfWork.UserRepository.Get(includeProperties: "UserRoles"))); }