public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Instructor instructor = await _context.Instructors .Include(i => i.CourseAssignments) .SingleAsync(i => i.ID == id); if (instructor == null) { return(RedirectToPage("./Index")); } var departments = await _context.Departments .Where(d => d.InstructorID == id) .ToListAsync(); departments.ForEach(d => d.InstructorID = null); _context.Instructors.Remove(instructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(string[] selectedCourses) { var newInstructor = new Instructor(); if (selectedCourses != null) { newInstructor.CourseAssignments = new List <CourseAssignment>(); foreach (var course in selectedCourses) { var courseToAdd = new CourseAssignment { CourseID = int.Parse(course) }; newInstructor.CourseAssignments.Add(courseToAdd); } } if (await TryUpdateModelAsync <Instructor>( newInstructor, "Instructor", i => i.FirstName, i => i.LastName, i => i.HireDate, i => i.OfficeAssignment)) { _context.Instructors.Add(newInstructor); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } PopulateAssignedCourseData(_context, newInstructor); return(Page()); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } var student = await _context.Students.FindAsync(id); //this line needs to be changed after changing StudentContext Student to Students if (student == null) { ErrorMsg = "Item does not exist anymore."; return(Page()); } try { _context.Students.Remove(student); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }catch (DbUpdateException) { return(RedirectToAction("./Delete", new { id, saveChangesError = true })); } }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { var emptyStudent = new Student(); // TryUpdateModel prevents overposting (security practice) if (await TryUpdateModelAsync <Student>( emptyStudent, "student", s => s.FirstName, s => s.LastName, s => s.EnrollmentDate)) { // Only the fields mentioned above are changed, no other field data can be posted. _context.Students.Add(emptyStudent); // Add method can not be asynchronous await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); /*if (!ModelState.IsValid) * { * return Page(); * } * * _context.Students.Add(Student); //this line needs to be changed after changing StudentContext Student to Students * await _context.SaveChangesAsync(); * * return RedirectToPage("./Index");*/ }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Departments.Add(Department); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(int?id) { if (id == null) { return(NotFound()); } Course = await _context.Courses.FindAsync(id); if (Course != null) { _context.Courses.Remove(Course); await _context.SaveChangesAsync(); } return(RedirectToPage("./Index")); }
// To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://aka.ms/RazorPagesCRUD. public async Task <IActionResult> OnPostAsync(int id) { var studentToUpdate = await _context.Students.FindAsync(id); if (studentToUpdate == null) { return(NotFound()); } if (await TryUpdateModelAsync <Student>( studentToUpdate, "student", s => s.FirstName, s => s.LastName, s => s.EnrollmentDate)) { await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); } return(Page()); }