Exemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var student = await _context.Students.FindAsync(id);

            if (student == null)
            {
                return(NotFound());
            }

            try
            {
                _context.Students.Remove(student);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateException ex)
            {
                _logger.LogError(ex, ErrorMessage);
                return(RedirectToAction("./Delete",
                                        new { id, saveChangesError = true }));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Instructor = await _context.Instructors
                         .Include(i => i.Courses)
                         .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"));
        }
Exemplo n.º 3
0
        // 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)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //_context.Attach(Department).State = EntityState.Modified;
            var departmentToUpdate = await _context.Departments
                                     .Include(i => i.Administrator)
                                     .FirstOrDefaultAsync(m => m.DepartmentID == id);

            if (departmentToUpdate == null)
            {
                return(HandleDeletedDepartment());
            }

            _context.Entry(departmentToUpdate).Property(
                d => d.ConcurrencyToken).OriginalValue = Department.ConcurrencyToken;

            if (await TryUpdateModelAsync <Department>(departmentToUpdate,
                                                       "Department", s => s.Name, s => s.StartDate, s => s.Budget, s => s.InstructorID))
            {
                try
                {
                    await _context.SaveChangesAsync();

                    return(RedirectToPage("./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. " +
                                                 "The department was deleted by another user.");
                        return(Page());
                    }

                    var dbValues = (Department)databaseEntry.ToObject();
                    await setDbErrorMessage(dbValues, clientValues, _context);

                    Department.ConcurrencyToken = (byte[])dbValues.ConcurrencyToken;
                    ModelState.Remove($"{nameof(Department)}.{nameof(Department.ConcurrencyToken)}");
                }
            }

            InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName",
                                              departmentToUpdate.InstructorID);

            return(Page());
        }
Exemplo n.º 4
0
        // To protect from overposting attacks, 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"));
        }
Exemplo n.º 5
0
        // 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)
        {
            #region old-code
            //if (!ModelState.IsValid)
            //{
            //    return Page();
            //}

            //_context.Attach(Course).State = EntityState.Modified;

            //try
            //{
            //    await _context.SaveChangesAsync();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //    if (!CourseExists(Course.CourseID))
            //    {
            //        return NotFound();
            //    }
            //    else
            //    {
            //        throw;
            //    }
            //}

            //return RedirectToPage("./Index");
            #endregion
            if (id == null)
            {
                return(NotFound());
            }

            var courseToUpdate = await _context.Courses.FindAsync(id);

            if (courseToUpdate == null)
            {
                return(NotFound());
            }

            if (await TryUpdateModelAsync <Course>(courseToUpdate,
                                                   "course", c => c.Credits, c => c.DepartmentID, c => c.Title))
            {
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }

            PopulateDepartmentsDropDownList(_context, courseToUpdate.DepartmentID);
            return(Page());
        }
Exemplo n.º 6
0
        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task <IActionResult> OnPostAsync()
        {
            var emptyStudent = new Student();

            if (await TryUpdateModelAsync <Student>(emptyStudent, "student",
                                                    s => s.FirstName, s => s.LastName, s => s.EnrollmentDate))
            {
                _context.Students.Add(emptyStudent);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }

            return(Page());
        }
Exemplo n.º 7
0
 public async Task <IActionResult> OnPostAsync(int id)
 {
     try
     {
         if (await _context.Departments.AnyAsync(m => m.DepartmentID == id))
         {
             _context.Departments.Remove(Department);
             await _context.SaveChangesAsync();
         }
         return(RedirectToPage("./Index"));
     }
     catch (DbUpdateConcurrencyException)
     {
         return(RedirectToPage("./Delete", new { concurrencyError = true, id = id }));
     }
 }
Exemplo n.º 8
0
        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task <IActionResult> OnPostAsync()
        {
            var emptyCourse = new Course();

            if (await TryUpdateModelAsync <Course>(emptyCourse, "course",
                                                   s => s.CourseID, s => s.DepartmentID, s => s.Title, s => s.Credits))
            {
                _context.Courses.Add(emptyCourse);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }

            PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID);
            return(Page());
        }
Exemplo n.º 9
0
        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"));
        }
Exemplo n.º 10
0
        // To protect from overposting attacks, see https://aka.ms/RazorPagesCRUD
        public async Task <IActionResult> OnPostAsync(string[] selectedCourses)
        {
            var newInstructor = new Instructor();

            if (selectedCourses.Length > 0)
            {
                newInstructor.Courses = new List <Course>();
                _context.Courses.Load();
            }

            //Add selected courses to th new instructor
            foreach (var course in selectedCourses)
            {
                var foundCourse = await _context.Courses.FindAsync(int.Parse(course));

                if (foundCourse != null)
                {
                    newInstructor.Courses.Add(foundCourse);
                }
                else
                {
                    _logger.LogWarning("Course {course} not found", course);
                }
            }

            try
            {
                if (await TryUpdateModelAsync <Instructor>(newInstructor,
                                                           "Instructor", i => i.FirstName, i => i.LastName,
                                                           i => i.HireDate, i => i.OfficeAssignmnet))
                {
                    _context.Instructors.Add(newInstructor);
                    await _context.SaveChangesAsync();

                    return(RedirectToPage("./Index"));
                }
                return(RedirectToPage("./Index"));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex.Message);
            }

            PopulateAssignedCourseData(_context, Instructor);
            return(Page());
        }
Exemplo n.º 11
0
        // 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, studentToUpdate => studentToUpdate.EnrollmentDate))
            {
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }

            return(Page());
        }
Exemplo n.º 12
0
        // 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, string[] selectedCourses)
        {
            if (id == null)
            {
                return(NotFound());
            }

            //_context.Attach(Instructor).State = EntityState.Modified;
            var instructorToUpdate = await _context.Instructors
                                     .Include(i => i.OfficeAssignmnet)
                                     .Include(i => i.Courses)
                                     .FirstOrDefaultAsync(s => s.ID == id);

            if (instructorToUpdate == null)
            {
                return(NotFound());
            }

            if (await TryUpdateModelAsync <Instructor>(instructorToUpdate,
                                                       "Instructor",
                                                       i => i.FirstName, i => i.LastName,
                                                       i => i.HireDate, i => i.OfficeAssignmnet))
            {
                if (String.IsNullOrWhiteSpace(
                        instructorToUpdate.OfficeAssignmnet?.Location))
                {
                    instructorToUpdate.OfficeAssignmnet = null;
                }
                UpdateInstructorCourses(selectedCourses, instructorToUpdate);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            UpdateInstructorCourses(selectedCourses, instructorToUpdate);
            PopulateAssignedCourseData(_context, instructorToUpdate);
            return(Page());
        }