public ActionResult Create(PersonnelProfileViewModel personnelViewModel) { // check if user with this email already exists for the current organisation var userExists = UserManager.FindByEmail(personnelViewModel.Personnel.Email); if (userExists != null) { ModelState.AddModelError("", string.Format("An account already exists for the email address {0}", personnelViewModel.Personnel.Email)); } if (ModelState.IsValid) { //Create Personnel personnelViewModel.Personnel = NidanBusinessService.CreatePersonnel(UserOrganisationId, personnelViewModel.Personnel); var result = CreateUserAndRole(personnelViewModel.Personnel); if (result.Succeeded) { return(RedirectToAction("Index")); } //delete the orphaned personnel & employment records NidanBusinessService.DeletePersonnel(UserOrganisationId, personnelViewModel.Personnel.PersonnelId); foreach (var error in result.Errors) { ModelState.AddModelError("", error); } } return(View(personnelViewModel)); }
public ActionResult Create(PersonnelProfileViewModel personnelViewModel) { // check if user with this email already exists for the current organisation var organisationId = UserOrganisationId; var centres = NidanBusinessService.RetrieveCentres(organisationId, e => true); var userExists = UserManager.FindByEmail(personnelViewModel.Personnel.Email); var roleId = NidanBusinessService.RetrieveAspNetRoles(organisationId, e => e.Id == personnelViewModel.Role).FirstOrDefault().Name; personnelViewModel.Centres = new SelectList(centres, "CentreId", "Name"); if (userExists != null) { ModelState.AddModelError("", string.Format("An account already exists for the email address {0}", personnelViewModel.Personnel.Email)); } if (ModelState.IsValid) { //Create Personnel personnelViewModel.Personnel.CentreId = personnelViewModel.Personnel.CentreId == 0 ? UserCentreId : personnelViewModel.Personnel.CentreId; personnelViewModel.Personnel = NidanBusinessService.CreatePersonnel(UserOrganisationId, personnelViewModel.Personnel); //var trainer = NidanBusinessService.CreateTrainer //create personnel //var personnel = new Personnel(){set all mandatory field like forename } //CreateTrainerUserAndRole(personnel) var result = CreateUserAndRole(personnelViewModel.Personnel, roleId); if (result.Succeeded) { return(RedirectToAction("Index")); } //delete the orphaned personnel & employment records NidanBusinessService.DeletePersonnel(UserOrganisationId, personnelViewModel.Personnel.PersonnelId); foreach (var error in result.Errors) { ModelState.AddModelError("", error); } } return(View(personnelViewModel)); }