public async Task <ActionResult> ProductUpsert(Product product, string removeId)
        {
            if (product.ProductId > 0)
            {
                Product newProduct = await _product.FindAsync(x => x.ProductId == product.ProductId);

                newProduct.Name        = product.Name;
                newProduct.CategoryId  = product.CategoryId;
                newProduct.PublisherId = product.PublisherId;
                newProduct.Author      = product.Author;
                newProduct.Size        = product.Size;
                newProduct.NumPage     = product.NumPage;
                newProduct.Price       = product.Price;
                newProduct.Description = product.Description;

                await _product.UpdateAsync(newProduct);

                string[] id      = removeId.Split(',');
                int      imageId = 0;
                foreach (var item in id)
                {
                    if (item == "0")
                    {
                        continue;
                    }
                    if (int.TryParse(item, out imageId))
                    {
                        await _productImage.DeleteByIdAsync(imageId);
                    }
                }
            }
            else
            {
                product.CreatedDate = DateTime.Now;
                await _product.CreateAsync(product);
            }

            var files = Request.Files.GetMultiple("imgFile");

            foreach (var item in files)
            {
                if (item != null && item.ContentLength > 0)
                {
                    string fileName   = Path.GetFileName(item.FileName);
                    string uploadPath = Server.MapPath("~/Assets/images/products/" + fileName);
                    item.SaveAs(uploadPath);

                    ProductImage productImage = new ProductImage()
                    {
                        ProductId = product.ProductId,
                        ImageURL  = fileName
                    };
                    await _productImage.CreateAsync(productImage);
                }
            }

            return(Redirect("/trang-quan-tri/quan-ly-san-pham"));
        }
Example #2
0
        public async Task <IHttpActionResult> Create(ProductApiModel product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var result = await _productServices.CreateAsync(AutoMapper.Mapper.Map <ProductDto>(product));

            return(CreatedAtRoute("DefaultApi", new { id = result.Id }, AutoMapper.Mapper.Map <ProductApiModel>(result)));
        }
        public async Task <ActionResult> Create([Bind(Include = "ProductID,ProductName,Image,Description,Price,SupplierID,CategoryID")] Product product)
        {
            try
            {
                product.Image = "";
                var f = Request.Files["ImageFile"];
                if (f != null && f.ContentLength > 0)
                {
                    string FileName   = System.IO.Path.GetFileName(f.FileName);
                    string UploadPath = Server.MapPath("~/wwwroot/images/" + FileName);
                    f.SaveAs(UploadPath);
                    product.Image = FileName;
                }
                await _product.CreateAsync(product);

                return(RedirectToAction("Index"));
            }
            catch (System.Exception)
            {
                ViewBag.CategoryID = new SelectList(_category.GetAll(), "CategoryID", "CategoryName", product.CategoryID);
                ViewBag.SupplierID = new SelectList(_supplier.GetAll(), "SupplierID", "SupplierName", product.SupplierID);
                return(View(product));
            }
        }