public async Task<ActionResult> CreateInvestigator(RegisterInvestigatorViewModel m) { if (ModelState.IsValid) { var UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); var user = new ApplicationUser { UserName = m.Username, Email = m.Email }; var result = await UserManager.CreateAsync(user, m.Password); if (result.Succeeded) { var person = new PX_Model.Person(); person.First_Name = m.FirstName; person.Last_Name = m.LastName; person.Email = m.Email; person.Phone_Number = m.PhoneNumber; person.Postcode = m.Postcode; person.State = m.State; person.Street = m.Street; person.Suburb = m.Suburb; person.Username = m.Username; _db.People.Add(person); _db.SaveChanges(); var investigator = new PX_Model.Investigator(); investigator.Person_Id = person.Id; investigator.Institution = m.Institution; _db.Investigators.Add(investigator); _db.SaveChanges(); return View("Index"); } foreach (var error in result.Errors) { ModelState.AddModelError("", error); } } return View(m); }
public async Task<ActionResult> CreateInvestigator(RegisterInvestigatorViewModel m) { //If inputs are valid if (ModelState.IsValid) { //Create a login account var UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); var user = new ApplicationUser { UserName = m.Username, Email = m.Email }; var result = await UserManager.CreateAsync(user, m.Password); //If we successfully created a login account if (result.Succeeded) { //Create a person in the database var person = new PX_Model.Person(); person.First_Name = m.FirstName; person.Last_Name = m.LastName; person.Email = m.Email; person.Phone_Number = m.PhoneNumber; person.Postcode = m.Postcode; person.State = m.State; person.Street = m.Street; person.Suburb = m.Suburb; person.Username = m.Username; _db.People.Add(person); _db.SaveChanges(); //Create an investigator in the database var investigator = new PX_Model.Investigator(); investigator.Person_Id = person.Id; investigator.Institution = m.Institution; _db.Investigators.Add(investigator); _db.SaveChanges(); //Go to admin dashboard return View("Index"); } foreach (var error in result.Errors) { ModelState.AddModelError("", error); } } //If error, show the form again with error messages on the top return View(m); }