public async Task <IActionResult> OnGetAsync(int?id, int?studentId) { if (id == null) { return(NotFound()); } //Show the LabEnrollments (Students) for the selected Lab. Lab = await _context.Lab .Include(c => c.LabEnrollments) .ThenInclude(s => s.Student) .AsNoTracking() .FirstOrDefaultAsync(m => m.LabID == id); if (Lab == null) { return(NotFound()); } //Remove the LabEnrollment from the database if (studentId != null) { LabEnrollment studentEnrollment = Lab.LabEnrollments.Where(x => x.LabID == id.Value && x.StudentID == studentId).First(); _context.LabEnrollment.Remove(studentEnrollment); await _context.SaveChangesAsync(); Response.Redirect("/Labs/Details/" + id.ToString()); } return(Page()); }
//OnPostAsync adds the LabEnrollments to the student selected. public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } if (!ModelState.IsValid) { return(Page()); } //Get the StudentID Student = await _context.Student.FindAsync(id); //If no labs are selected, throw an error message. if (LabIDs.Length == 0) { ModelState.AddModelError("No Lab Selected", "You must select at least one lab to enroll this student in"); return(Page()); } //Loop through the LabEnrollments and enroll the student in the selected labs. foreach (int LabId in LabIDs) { LabEnrollment studentEnrollment = new LabEnrollment { StudentID = Student.StudentID, LabID = LabId }; _context.LabEnrollment.Add(studentEnrollment); } await _context.SaveChangesAsync(); return(RedirectToPage("/Students/Index")); }
//Add LabEnrollments for selected students. public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } if (!ModelState.IsValid) { return(Page()); } Lab = await _context.Lab.FindAsync(id); //If no student is selected, throw an error message. if (StudentIDs.Length == 0) { ModelState.AddModelError("No Student Selected", "You must select at least one student to enroll in this lab."); return(Page()); } //Loop through the selected StudentIDs and add the enrollments. foreach (int StudentId in StudentIDs) { LabEnrollment studentEnrollment = new LabEnrollment { LabID = Lab.LabID, StudentID = StudentId }; _context.LabEnrollment.Add(studentEnrollment); } //Save changes await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
//OnPostAsync launches the lab, and handles all inputs from the users public async Task <IActionResult> OnPostAsync(int?id, string command) { //If the "End Lab" button is clicked, end the lab and sign out the instructor. Redirect to the login page. if (command.Equals("EndLab")) { await HttpContext.SignOutAsync(); return(RedirectToPage("/Index")); } Lab = await _context.Lab.Where(l => l.LabID == id).FirstAsync(); //Find LabEnrollment when student signs in or out of lab. LabEnrollment LabEnrollment = await _context.LabEnrollment.Where(e => e.LabID == id && e.StudentID == StudentID).FirstOrDefaultAsync(); //if the student has not been enrolled in the lab, throw an error message. Calls the LabEnrollmentExists method if (LabEnrollment == null || !LabEnrollmentExists(LabEnrollment.LabEnrollmentID)) { ModelState.AddModelError("Error", "You have not been enrolled in this lab, please contact your instructor."); return(Page()); } //If the student successfully signs into lab, add the DateTime.Now stamp in LabEnrollments if (command.Equals("Login")) { //If the student tries to sign in after already signing in, throw an error message. if (LabEnrollment.LabSignIn != null) { ModelState.AddModelError("Error", "You have already signed into this lab."); return(Page()); } LabEnrollment.LabSignIn = DateTime.Now; _context.LabEnrollment.Update(LabEnrollment); SuccessLogin = true; } //If the student successfully signs out of the lab, add the DateTime.Now stamp in LabEnrollments if (command.Equals("Logout")) { //If the student tries to sign out after already signing out, throw an error message. if (LabEnrollment.LabSignOut != null) { ModelState.AddModelError("Error", "You have already signed out of this lab."); return(Page()); } LabEnrollment.LabSignOut = DateTime.Now; _context.LabEnrollment.Update(LabEnrollment); SuccessLogout = true; } //Save changes await _context.SaveChangesAsync(); return(Page()); }
//The OnPostAsync removes the LabEnrollment public async Task <IActionResult> OnPostAsync(int?id, int labId) { if (id == null) { return(NotFound()); } Student = await _context.Student.FindAsync(id); LabEnrollment studentEnrollment = _context.LabEnrollment .Where(le => le.StudentID == id && le.LabID == labId).FirstOrDefault(); _context.LabEnrollment.Remove(studentEnrollment); //Save changes await _context.SaveChangesAsync(); return(RedirectToPage("./Details")); }