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

            var customer = await _context.Customers
                           .AsNoTracking()
                           .FirstOrDefaultAsync(m => m.ID == id);

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

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

            var customerToUpdate = await _context.Customers.FindAsync(id);

            if (await TryUpdateModelAsync <Customer>(
                    customerToUpdate,
                    "customer",
                    c => c.FirstName, c => c.LastName, c => c.PhoneNumber, c => c.EmailAddress, c => c.SexualOrientation, c => c.MembershipPlan))
            {
                await _context.SaveChangesAsync();

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

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

            var emptyCustomer = new Customer();

            if (await TryUpdateModelAsync <Customer>(
                    emptyCustomer,
                    "customer", //Prefix for form value.
                    s => s.FirstName, s => s.LastName, s => s.PhoneNumber, s => s.EmailAddress, s => s.SexualOrientation, s => s.MembershipPlan))
            {
                _context.Customers.Add(emptyCustomer);
                await _context.SaveChangesAsync();

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

            return(null);
        }