예제 #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 */)
            {
                //Log the error (uncomment ex variable name and write a log.)
                return(RedirectToAction("./Delete",
                                        new { id, saveChangesError = true }));
            }
        }
예제 #2
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());
        }
예제 #3
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"));
        }
예제 #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()
        {
            var emptyStudent = new Student();

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

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

            return(Page());
        }
예제 #5
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());
        }
예제 #6
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 }));
     }
 }