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

            //Instructor = await _context.Instructors.FindAsync(id);

            //if (Instructor != null)
            //{
            //    _context.Instructors.Remove(Instructor);
            //    await _context.SaveChangesAsync();
            //}

            //return RedirectToPage("./Index");
            Instructor instructor = await _context.Instructors
                                    .Include(i => i.CourseAssignments)
                                    .SingleAsync(i => i.ID == id);

            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.º 2
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

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

            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;
                }
                await _context.SaveChangesAsync();
            }
            return(RedirectToPage("./Index"));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync(string[] selectedCourses)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            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(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());
        }
Exemplo n.º 4
0
        public async Task <IActionResult> OnPostAsync()
        {
            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"));
        }
Exemplo n.º 5
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var student = await _context.Student
                          .AsNoTracking()
                          .FirstOrDefaultAsync(m => m.ID == id);

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

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

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateException /* ex */)
            {
                //Log the error (uncomment ex variable name and write a log.)
                return(RedirectToAction("./Delete",
                                        new { id, saveChangesError = true }));
            }
        }
Exemplo n.º 6
0
        public async Task <IActionResult> OnPostAsync(int?id, string[] selectedCourses)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

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

            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());
        }
Exemplo n.º 7
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            // Находим нужного студента
            var student = await _context.Student
                          .AsNoTracking()
                          .FirstOrDefaultAsync(m => m.ID == id);

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

            try
            {
                // Удаляем студента
                _context.Student.Remove(student);

                // Сохраняем удаление
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateException /* ex */)
            {
                // Редирект с уведомлением о ошибке saveChangesError = true означет
                // что OnGetAsync выведет ErrorMessage = "Delete failed. Try again";
                return(RedirectToAction("./Delete", new { id, saveChangesError = true }));
            }
        }
Exemplo n.º 8
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // performing a validation on the new course to see if there is a relationship
            // between the course created and the existing department
            var emptyCourse = new Course();

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

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

            // if the TryUpdateModelAsync fails select the departmentID
            PopulateDepartmentDropDownList(_context, emptyCourse.DepartmentID);
            return(Page());
        }
        // 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.Course.Add(Course);
            // await _context.SaveChangesAsync();

            // return RedirectToPage("./Index");
            var emptyCourse = new Course();

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

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

            PopulateDepartmentDropDownList(_context, emptyCourse.DepartmentID);
            return(Page());
        }
Exemplo n.º 10
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            //_context.Student.Add(Student);
            //await _context.SaveChangesAsync();

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

            var emptyStudent = new Student();

            if (await TryUpdateModelAsync <Student>(
                    emptyStudent,
                    "student", // Prefix for form value.
                    s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
            {
                _context.Student.Add(emptyStudent);
                await _context.SaveChangesAsync();

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

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

            var student = await _context.Student.AsNoTracking().FirstOrDefaultAsync(m => m.ID == id).ConfigureAwait(false);

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

            try
            {
                _context.Student.Remove(Student);
                await _context.SaveChangesAsync().ConfigureAwait(false);

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateException ex)
            {
                return(RedirectToAction("./Delete", new { id, saveChangesError = true }));
            }
        }
Exemplo n.º 12
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }


            // TryUpdateModelAsync<Student> пытается обновить объект emptyStudent,
            // используя отправленные значения формы из свойства PageContext в PageModel.
            // TryUpdateModelAsync обновляет только перечисленные свойства
            // (s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate).
            // Второй аргумент("student", // Prefix) представляет собой префикс для поиска значений.
            // Задается без учета регистра символов.
            // Отправленные значения формы преобразуются в типы в модели Student с использованием привязки модели https://docs.microsoft.com/ru-ru/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2#how-model-binding-works .
            var emptyStudent = new Student();

            if (await TryUpdateModelAsync <Student>(
                    emptyStudent,
                    "student", // Prefix for form value.
                    s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
            {
                _context.Student.Add(emptyStudent);
                await _context.SaveChangesAsync();

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

            return(null);
        }
Exemplo n.º 13
0
        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"));

            /*var emptyStudent = new Student();
             * if(await TryUpdateModelAsync<Student>(
             *  emptyStudent,
             *  "student",              // Prefix for entered on form values
             *  s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
             * {
             *  _context.Student.Add(emptyStudent);
             *  await _context.SaveChangesAsync();
             *  return RedirectToPage("./Index");
             * }
             * return null;    */
        }
Exemplo n.º 14
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            Student = new Student();
            Student.CopyPropertiesFrom(StudentVM);
            _context.Attach(Student).State = EntityState.Modified;

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentVMExists(StudentVM.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 15
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 (id == null)
            {
                return(NotFound());
            }

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

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

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

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

            // Select DepartmentID if TryUpdateModelAsync fails.
            PopulateDepartmentDropDownList(_context, courseToUpdate.DepartmentID);
            return(Page());
        }
Exemplo n.º 16
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var emptyCourse = new Course();

            if (await TryUpdateModelAsync <Course>( //to prevent overposting.
                    emptyCourse,
                    "course",                       //Prefix for form value.
                    s => s.CourseID, s => s.DepartmentID, s => s.Credits))
            {
                _context.Course.Add(emptyCourse);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            //   _context.Course.Add(Course);
            //   await _context.SaveChangesAsync();
            // Select DepartmentID if TryUpdateModelAsync fails.
            PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID);
            return(Page());

            //return RedirectToPage("./Index");
        }
Exemplo n.º 17
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var studentToUpdate = await _context.Student.FindAsync(id);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!StudentExists(Student.ID))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 18
0
        public async Task <IActionResult> OnPostAsync(int?id, string[] selectedCourses)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // _context.Attach(Instructor).State = EntityState.Modified;
            // find a matching instructor first
            var instructorToUpdate = await _context.Instructors
                                     .Include(i => i.OfficeAssignment)
                                     .Include(i => i.CourseAssignments)
                                     .ThenInclude(i => i.Course)
                                     .FirstOrDefaultAsync(s => s.ID == id);

            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;
                }
                UpdateInstructorsCourses(_context, selectedCourses, instructorToUpdate);
                await _context.SaveChangesAsync();

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


            //try
            //{
            //    await _context.SaveChangesAsync();
            //}
            //catch (DbUpdateConcurrencyException)
            //{
            //    if (!InstructorExists(Instructor.ID))
            //    {
            //        return NotFound();
            //    }
            //    else
            //    {
            //        throw;
            //    }
            //}

            //return RedirectToPage("./Index");
        }
Exemplo n.º 19
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Course.Add(Course);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 20
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(await HandleDeletedDepartment());
            }

            _context.Entry(departmentToUpdate).Property("RowVersion").OriginalValue = Department.RowVersion;
            //The Department.RowVersion is set when the record is read from the database on the initial GET.
            //if 2RowVersion2 has been updated so it doesn't match Department.RowVersion then Concurrency exception is thrown.
            //OriginalValue is the database value just before we tried to save

            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.RowVersion = (byte[])dbValues.RowVersion;                    //sets the rowversion to be the correct one from the database
                    ModelState.Remove("Department.RowVersion");
                }
            }
            InstructorNameSL = new SelectList(_context.Instructors, "ID", "FullName", departmentToUpdate.InstructorID);
            return(Page());
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Create([Bind("FirstMidName,HireDate,LastName,OfficeAssignment")] Instructor instructor, string[] selectedCourses)
        {
            if (selectedCourses != null)
            {
                instructor.CourseAssignments = new List <CourseAssignment>();
                foreach (var course in selectedCourses)
                {
                    var courseToAdd = new CourseAssignment {
                        InstructorID = instructor.ID, CourseID = int.Parse(course)
                    };
                    instructor.CourseAssignments.Add(courseToAdd);
                }
            }
            if (ModelState.IsValid)
            {
                _context.Add(instructor);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            PopulateAssignedCourseData(instructor);
            return(View(instructor));
        }
Exemplo n.º 22
0
        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"));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }


            {
                _context.Student.Add(EmptyStudent);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
        }
Exemplo n.º 24
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }


            //overposting protection version 2
            var newStudent = _context.Add(new Student());

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

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 25
0
        //public async Task<IActionResult> OnPostAsync()
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        return Page();
        //    }

        //    var emptyStudent = new Student();


        //    if (await TryUpdateModelAsync<Student>(
        //        emptyStudent,
        //        "student",   // 是用于查找值的前缀。 该自变量不区分大小写
        //        s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
        //        {
        //            _context.Student.Add(emptyStudent);
        //            await _context.SaveChangesAsync();
        //            return RedirectToPage("./Index");
        //        }

        //    return null;
        //}



        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

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

            //SetValues 使用属性名称匹配。 视图模型类型(StudentVM)不需要与模型类型(Student)相关,它只需要具有匹配的属性。
            entry.CurrentValues.SetValues(StudentVM);

            await _context.SaveChangesAsync();

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

            Enrollment = await _context.Enrollment.FindAsync(id);

            if (Enrollment != null)
            {
                _context.Enrollment.Remove(Enrollment);
                await _context.SaveChangesAsync();
            }

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

            Instructor = await _context.Instructors.FindAsync(id);

            if (Instructor != null)
            {
                _context.Instructors.Remove(Instructor);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Course = await _context.Course.AsNoTracking().FirstOrDefaultAsync(m => m.CourseID == id);

            if (Course != null)
            {
                _context.Course.Remove(Course);
                await _context.SaveChangesAsync();
            }

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

            Room = await _context.Room.FindAsync(id);

            if (Room != null)
            {
                _context.Room.Remove(Room);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Exemplo n.º 30
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"));
        }