Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            // USING VM MODEL
            //var entry = _context.Add(new Student());
            //entry.CurrentValues.SetValues(StudentVM);
            //await _context.SaveChangesAsync();
            //return RedirectToPage("./Index");

            //USE THIS CODE TO PREVENT OVERPOSTING

            var emptyStudent = new Student();

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

                return(RedirectToPage("./Index"));
            }
            return(null);
        }
Ejemplo n.º 2
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.CourseAssignments)
                                     .ThenInclude(i => i.Course)
                                     .FirstOrDefaultAsync(s => s.InstructorID == id);

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

                return(RedirectToPage("./Index"));
            }
            UpdateInstrcutorCourses(_context, selectedCourses, instructorToUpdate);
            PopulateAssignedCourseData(_context, instructorToUpdate);
            return(Page());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

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

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

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

                return(RedirectToPage("./Index"));
            }
            catch (DbUpdateException)
            {
                //Log  the error
                return(RedirectToAction("/.Delete",
                                        new { id, saveChangesError = true }));
            }
        }
Ejemplo n.º 4
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 department is null, Department has already been deleted by another user
            if (departmentToUpdate == null)
            {
                return(HandleDeleteDepartment());
            }

            _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 deparment was deleted by another user.");
                        return(Page());
                    }

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

                    Department.RowVersion = (byte[])dbValues.RowVersion;
                    // To Clear the model error for the next post request.
                    ModelState.Remove("Department.RowVersion");
                }
            }
            InstructorNameSL = new SelectList(_context.Instructors,
                                              "DepartmentID", "FullName", departmentToUpdate.InstructorID);
            return(Page());
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Departments.Add(Department);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> OnPostAsync(int id)
        {
            Instructor instructorToRemove = await _context.Instructors
                                            .Include(i => i.CourseAssignments)
                                            .SingleAsync(i => i.InstructorID == id);

            var departments = await _context.Departments
                              .Where(d => d.InstructorID == id)
                              .ToListAsync();

            departments.ForEach(d => d.InstructorID = null);
            _context.Instructors.Remove(instructorToRemove);
            await _context.SaveChangesAsync();

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

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

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

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

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

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

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

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

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

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

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

            var emptyCourse = new Course();

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

                return(RedirectToPage("./Index"));
            }
            // Select DepartmentID if TryUpdateModelAsync fails
            PopulateDepartmentsDropDownList(_context, emptyCourse.DepartmentID);
            return(Page());
        }
Ejemplo n.º 12
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 <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());
        }