// GET: Instructors public async Task <IActionResult> Index(int?id, int?courseID) { var viewModel = new InstructorIndexData(); viewModel.Instructors = await _context.Instructors .Include(i => i.OfficeAssignment) .Include(i => i.CourseAssignments) .ThenInclude(i => i.Course) .ThenInclude(i => i.Department) .OrderBy(i => i.LastName) .ToListAsync(); if (id != null) { ViewData["InstructorID"] = id.Value; Instructor instructor = viewModel.Instructors.Where( i => i.ID == id.Value).Single(); viewModel.Courses = instructor.CourseAssignments.Select(s => s.Course); } if (courseID != null) { ViewData["CourseID"] = courseID.Value; var selectedCourse = viewModel.Courses.Where(x => x.CourseID == courseID).Single(); await _context.Entry(selectedCourse).Collection(x => x.Enrollments).LoadAsync(); foreach (Enrollment enrollment in selectedCourse.Enrollments) { await _context.Entry(enrollment).Reference(x => x.Student).LoadAsync(); } viewModel.Enrollments = selectedCourse.Enrollments; } return(View(viewModel)); }
public async Task <IActionResult> PutEvent([FromRoute] int id, [FromBody] Event @event) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != @event.Id) { return(BadRequest()); } _context.Entry(@event).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!EventExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> Edit(int?id, byte[] rowVersion) { if (id == null) { return(NotFound()); } var departmentToUpdate = await _context.Departments.Include(i => i.Administrator).SingleOrDefaultAsync(m => m.DepartmentID == id); if (departmentToUpdate == null) { Department deletedDepartment = new Department(); await TryUpdateModelAsync(deletedDepartment); ModelState.AddModelError(string.Empty, "Unable to save changes. The department was deleted by another user."); ViewData["InstructorID"] = new SelectList(_context.Instructors, "ID", "FullName", deletedDepartment.InstructorID); return(View(deletedDepartment)); } _context.Entry(departmentToUpdate).Property("RowVersion").OriginalValue = rowVersion; if (await TryUpdateModelAsync <Department>( departmentToUpdate, "", s => s.Name, s => s.StartDate, s => s.Budget, s => s.InstructorID)) { try { await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException ex) { var exceptionEntry = ex.Entries.Single(); var clientValues = (Department)exceptionEntry.Entity; var databaseEntry = exceptionEntry.GetDatabaseValues(); if (databaseEntry == null) { ModelState.AddModelError(string.Empty, "Unable to save changes. The department was deleted by another user."); } else { var databaseValues = (Department)databaseEntry.ToObject(); if (databaseValues.Name != clientValues.Name) { ModelState.AddModelError("Name", $"Current value: {databaseValues.Name}"); } if (databaseValues.Budget != clientValues.Budget) { ModelState.AddModelError("Budget", $"Current value: {databaseValues.Budget:c}"); } if (databaseValues.StartDate != clientValues.StartDate) { ModelState.AddModelError("StartDate", $"Current value: {databaseValues.StartDate:d}"); } if (databaseValues.InstructorID != clientValues.InstructorID) { Instructor databaseInstructor = await _context.Instructors.SingleOrDefaultAsync(i => i.ID == databaseValues.InstructorID); ModelState.AddModelError("InstructorID", $"Current value: {databaseInstructor?.FullName}"); } ModelState.AddModelError(string.Empty, "The record you attempted to edit " + "was modified by another user after you got the original value. The " + "edit operation was canceled and the current values in the database " + "have been displayed. If you still want to edit this record, click " + "the Save button again. Otherwise click the Back to List hyperlink."); departmentToUpdate.RowVersion = (byte[])databaseValues.RowVersion; ModelState.Remove("RowVersion"); } } } ViewData["InstructorID"] = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID); return(View(departmentToUpdate)); }