示例#1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Student = await _context.Students.FindAsync(id);

            if (Student == null)
            {
                return(NotFound());
            }
            try
            {
                _context.Students.Remove(Student);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateConcurrencyException)
            {
                return(RedirectToAction("./Delete",
                                        new { id, saveChangesError = true }));
            }
        }
示例#2
0
        // To protect from overposting attacks, please 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 (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());
        }
示例#3
0
        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"));
        }
示例#4
0
        // To protect from overposting attacks, please 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.FirstMidName, i => i.LastName,
                    i => i.HireDate, i => i.OfficeAssignment))
            {
                _context.Instructors.Add(newInstructor);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            PopulateAssignedCourseData(_context, newInstructor);
            return(Page());
        }
示例#5
0
        public async Task <IActionResult> OnPostAsync(int id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var departmentToUpdate = await _context.Departments
                                     .Include(i => i.Administrator)
                                     .FirstOrDefaultAsync(m => m.DepartmentID == id);

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

            _context.Entry(departmentToUpdate)
            .Property("RowVersion").OriginalValue = Department.RowVersion;

            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);

                    // Save the current RowVersion so next postback
                    // matches unless an new concurrency issue happens.
                    Department.RowVersion = (byte[])dbValues.RowVersion;
                    // Clear the model error for the next postback.
                    ModelState.Remove("Department.RowVersion");
                }
            }

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

            return(Page());
        }
示例#6
0
        // To protect from overposting attacks, please 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"));
        }
示例#7
0
        // To protect from overposting attacks, please 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());
            }

            var entry = _context.Add(new Student());

            entry.CurrentValues.SetValues(StudentVM);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
示例#8
0
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for
        // more details see https://aka.ms/RazorPagesCRUD.
        public async Task <IActionResult> OnPostAsync()
        {
            var emptyCourse = new Course();

            if (await TryUpdateModelAsync(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());
        }
示例#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"));
        }
示例#10
0
        // To protect from overposting attacks, please 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.FirstMidName, s => s.LastName,
                                                    s => s.EnrollmentDate))
            {
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            return(Page());
        }
示例#11
0
 public async Task <IActionResult> OnPostAsync(int id)
 {
     try
     {
         if (await _context.Departments.AnyAsync(
                 m => m.DepartmentID == id))
         {
             // Department.rowVersion value is from when the entity
             // was fetched. If it doesn't match the DB, a
             // DbUpdateConcurrencyException exception is thrown.
             _context.Departments.Remove(Department);
             await _context.SaveChangesAsync();
         }
         return(RedirectToPage("./Index"));
     }
     catch (DbUpdateConcurrencyException)
     {
         return(RedirectToPage("./Delete",
                               new { concurrencyError = true, id = id }));
     }
 }
示例#12
0
        // To protect from overposting attacks, please 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());
            }

            var instructorToUpdate = await _context.Instructors
                                     .Include(i => i.OfficeAssignment)
                                     .Include(i => i.CourseAssignments)
                                     .ThenInclude(i => i.Course)
                                     .FirstOrDefaultAsync(s => s.ID == id);

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

            if (await TryUpdateModelAsync <Instructor>(instructorToUpdate, "Instructor", i => i.FirstMidName,
                                                       i => i.LastName, i => i.HireDate, i => i.OfficeAssignment))
            {
                if (string.IsNullOrWhiteSpace(instructorToUpdate.OfficeAssignment.Location))
                {
                    instructorToUpdate.OfficeAssignment = null;
                }

                UpdateInstructorCourses(_context, selectedCourses, instructorToUpdate);
                await _context.SaveChangesAsync();

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

            UpdateInstructorCourses(_context, selectedCourses, instructorToUpdate);
            PopulateAssignedCourseData(_context, instructorToUpdate);
            return(Page());
        }