public ActionResult CreateStudent([Bind(Include = "Id,FirstName,LastName,Email,Password,CourseModel_Id")] CreateSudentViewModel createSudentViewModel) { var course = context.Courses.FirstOrDefault(d => d.Id == createSudentViewModel.CourseModel_Id); if (ModelState.IsValid) { UserStore <Models.Identity.ApplicationUser> userStore = new UserStore <Models.Identity.ApplicationUser>(context); UserManager <Models.Identity.ApplicationUser> userManager = new UserManager <Models.Identity.ApplicationUser>(userStore); Models.Identity.ApplicationUser user = new Models.Identity.ApplicationUser { UserName = createSudentViewModel.Email, Email = createSudentViewModel.Email, FirstName = createSudentViewModel.FirstName, LastName = createSudentViewModel.LastName }; var result = userManager.Create(user, createSudentViewModel.Password); if (!result.Succeeded) { throw new Exception(string.Join("\n", result.Errors)); } userManager.AddToRole(user.Id, "Student"); course.Students.Add(user); TempData["Message"] = "Succsessfully added a student"; context.SaveChanges(); } return(RedirectToAction("_StudentListPartial", new { id = course.Id })); }
public ActionResult AddTeacher(RegisterViewModel arg) { if (arg == null) { return(HttpNotFound()); } //Add contents of arg to a new post in the database UserStore <Models.Identity.ApplicationUser> userStore = new UserStore <Models.Identity.ApplicationUser>(context); UserManager <Models.Identity.ApplicationUser> userManager = new UserManager <Models.Identity.ApplicationUser>(userStore); ApplicationUser user = new Models.Identity.ApplicationUser { UserName = arg.Email, Email = arg.Email, FirstName = arg.FirstName, LastName = arg.LastName }; var result = userManager.Create(user, arg.Password); if (!result.Succeeded) { foreach (var error in result.Errors) { ModelState.AddModelError("", error); } return(RedirectToAction("_TeacherListPartial")); } userManager.AddToRole(user.Id, "Teacher"); context.SaveChanges(); return(RedirectToAction("_TeacherListPartial")); }