示例#1
0
        public IActionResult Upsert(int?id)
        {
            ProductViewModel productVM = new ProductViewModel()
            {
                Product      = new BulkyBook.Models.Product(),
                CategoryList = _categoryRepository.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString(),
                }),
                CoverTypeList = _coverTypeRepository.GetAll().Select(i => new SelectListItem
                {
                    Text  = i.Name,
                    Value = i.Id.ToString()
                })
            };

            BulkyBook.Models.Product product = new BulkyBook.Models.Product(); //SOME ERROR with Single class initialization.
            if (id == null)
            {
                // Create
                return(View(productVM));
            }
            // Update
            productVM.Product = _productRepository.GetById(id.GetValueOrDefault());
            if (productVM.Product == null)
            {
                // Incorrect ID
                return(NotFound());
            }
            return(View(productVM));
        }
示例#2
0
        public IActionResult Upsert(ProductViewModel productVM)
        {
            if (ModelState.IsValid)
            {
                // for image upload we need root path.
                // images will be saved under root/images/products
                string webRootPath = _hostEnvironment.WebRootPath;
                var    files       = HttpContext.Request.Form.Files;
                if (files.Count > 0)
                {
                    string fileName  = Guid.NewGuid().ToString();
                    var    uploads   = Path.Combine(webRootPath, @"images\products");
                    var    extension = Path.GetExtension(files[0].FileName);

                    if (productVM.Product.ImageUrl != null)
                    {
                        // this is update
                        var imagePath = Path.Combine(webRootPath, productVM.Product.ImageUrl.TrimStart('\\'));
                        if (System.IO.File.Exists(imagePath))
                        {
                            System.IO.File.Delete(imagePath);
                        }
                    }
                    using (var fileStreams = new FileStream(Path.Combine(uploads, fileName + extension), FileMode.Create))
                    {
                        files[0].CopyTo(fileStreams);
                    }
                    productVM.Product.ImageUrl = @"\images\products\" + fileName + extension;
                }
                else
                {
                    //update when they do not change the image
                    if (productVM.Product.Id != 0)
                    {
                        BulkyBook.Models.Product objFromDb = _productRepository.GetById(productVM.Product.Id);
                        productVM.Product.ImageUrl = objFromDb.ImageUrl;
                    }
                }

                if (productVM.Product.Id == 0)
                {
                    _productRepository.Add(productVM.Product);
                }
                else
                {
                    _productRepository.Update(productVM.Product);
                }
                _productRepository.Commit();
                return(RedirectToAction(nameof(Index))); // action name
            }
            return(View(productVM));
        }