Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(int[] Sizes, int id, Product product)
        {
            if (id != product.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(product);

                    var currentSizes = _context.ProductSizes.Where(x => x.ProductId == id);
                    var newSizes     = Sizes.Select(x => new ProductSizes {
                        ProductId = id, SizeId = x
                    });
                    _context.UpdateManyToMany(currentSizes, newSizes, x => x.SizeId);


                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProductExists(product.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Id", product.CategoryId);
            ViewData["ColorId"]    = new SelectList(_context.Colors, "Id", "Id", product.ColorId);
            ViewData["GenderId"]   = new SelectList(_context.Genders, "Id", "Id", product.GenderId);
            return(View(product));
        }