public async Task <ActionResult> Create(ScheduledClassViewModel scvm) { if (ModelState.IsValid) { var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(_db)); var scheduledClass = new ScheduledClass { Id = Guid.NewGuid().ToString(), Instructor = userManager.Users.FirstOrDefault(u => u.Id == scvm.InstructorId), ClassStartTime = scvm.StartDateTime, ScheduledClassType = _db.ScheduledClassTypes.FirstOrDefault(x => x.Name == scvm.ScheduledClassType.Name) }; try { _db.ScheduledClasses.Add(scheduledClass); await _db.SaveChangesAsync(); } catch (Exception ex) { _logger.Error(ex); } return(RedirectToAction("Index")); } return(View()); }
// GET: ScheduledClass/Create public ActionResult Create(string type, DateTime start) { var userManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(_db)); var instructorRoleId = _db.Roles.FirstOrDefault(r => r.Name == RoleNameHelper.InstructorName)?.Id; var instructors = userManager.Users.Where(x => x.Roles.Any(s => s.RoleId == instructorRoleId)).ToList(); var classType = _db.ScheduledClassTypes.FirstOrDefault(x => x.Name == type); var vm = new ScheduledClassViewModel { InstructorList = instructors, StartDateTime = start, ScheduledClassType = classType }; return(View(vm)); }