Example #1
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()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Category).State = EntityState.Modified;

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

            return(RedirectToPage("./Index"));
        }
        // 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, string[] selectedCategories)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Attach(Product).State = EntityState.Modified;

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

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

            var productToUpdate = await _context.Product
                                  .Include(p => p.Make)
                                  .Include(p => p.ProductCategories)
                                  .ThenInclude(p => p.Category)
                                  .FirstOrDefaultAsync(s => s.ID == id);

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

            if (await TryUpdateModelAsync <Product>(
                    productToUpdate,
                    "Product",
                    p => p.Name, p => p.Description,
                    p => p.PublishingDate, p => p.Image, p => p.Price))
            {
                UpdatePrductCategories(_context, selectedCategories, productToUpdate);
                await _context.SaveChangesAsync();

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

            UpdatePrductCategories(_context, selectedCategories, productToUpdate);
            PopulateSelectedCategoriesList(_context, productToUpdate);
            return(RedirectToPage("./Index"));
        }