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

            var product = await _context.Product.AsNoTracking().FirstOrDefaultAsync(p => p.ProductID == id);

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

            try
            {
                _context.Product.Remove(product);
                await _context.SaveChangesAsync();

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

            var emptyProduct = new Product();

            if (await TryUpdateModelAsync <Product>(
                    emptyProduct,
                    "product",               // Prefix for form value.
                    s => s.CategoryID, s => s.Model, s => s.Price, s => s.Brand, s => s.DateOfCreation, s => s.Description))
            {
                _context.Product.Add(emptyProduct);
                await _context.SaveChangesAsync();

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

            PopulateCategoriesDropDownList(_context, emptyProduct.CategoryID);
            return(Page());
        }