示例#1
0
        public async Task <IActionResult> AddQuantity(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var product = await _context.Products.FirstOrDefaultAsync(p => p.ID == id);

            if (product == null)
            {
                return(NotFound());
            }
            var model = new ProductAddQuantityViewModel
            {
                ID        = product.ID,
                Ten       = product.Ten,
                SoLuongCo = product.SoLuong
            };

            return(View(model));
        }
示例#2
0
        public async Task <IActionResult> AddQuantity(int id, [Bind("ID,Ten,SoLuongCo,SoLuongThem")] ProductAddQuantityViewModel model)
        {
            if (id != model.ID)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                var product = await _context.Products.FirstOrDefaultAsync(p => p.ID == id);

                if (product == null)
                {
                    return(NotFound());
                }
                product.SoLuong += model.SoLuongThem;
                _context.Products.Update(product);
                await _context.SaveChangesAsync();

                TempData["messageSuccess"] = $"Sản phẩm \"{product.Ten}\" đã cập nhật số lượng từ \"{model.SoLuongCo}\" đến \"{product.SoLuong}\"";
                return(RedirectToAction("Details", "Products", new { id = id }));
            }
            return(View(model));
        }