// 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"));
        }
Exemple #2
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"));
        }
Exemple #3
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.Make.Add(Make);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
Exemple #4
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Make = await _context.Make.FindAsync(id);

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

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Product = await _context.Product.FindAsync(id);

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

            return(RedirectToPage("./Index"));
        }
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            Category = await _context.Category.FindAsync(id);

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

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


            var newProduct = new Product();

            if (selectedCategories != null)
            {
                newProduct.ProductCategories = new List <ProductCategory>();
                foreach (var category in selectedCategories)
                {
                    var catToAdd = new ProductCategory
                    {
                        CategoryID = int.Parse(category)
                    };
                    newProduct.ProductCategories.Add(catToAdd);
                }
            }

            if (await TryUpdateModelAsync <Product>(
                    newProduct, "Product",
                    p => p.Name, p => p.Description,
                    p => p.PublishingDate, p => p.Image, p => p.Price,
                    p => p.MakeID
                    ))
            {
                _context.Product.Add(newProduct);
                await _context.SaveChangesAsync();

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

            PopulateSelectedCategoriesList(_context, newProduct);
            return(Page());
        }