コード例 #1
0
        public IActionResult AddProduct()
        {
            ProductInputVM vm = new ProductInputVM();

            var brands     = _reposBrand.GetBrands();
            var categories = _reposCategory.GetCategories();

            vm.Brands     = brands.Select(b => new SelectListItem(b.Name, b.Id.ToString())).ToList();
            vm.Categories = categories.Select(c => new SelectListItem(c.Name, c.Id.ToString())).ToList();
            return(View(vm));
        }
コード例 #2
0
        public IActionResult UpdateProduct(ProductInputVM vm)
        {
            if (ModelState.IsValid)
            {
                Product product = _reposProduct.GetProduct(vm.Id);
                product.Name       = vm.Name;
                product.Price      = vm.Price;
                product.Quantity   = vm.Quantity;
                product.BrandId    = vm.BrandId.Value;
                product.CategoryId = vm.CategoryId.Value;

                string FileName = null;

                if (vm.Photo != null)
                {
                    if (vm.ExistingPhotoPath != null)
                    {
                        string filePathDel = Path.Combine(_hostingEnvironment.WebRootPath, "images", vm.ExistingPhotoPath);
                        System.IO.File.Delete(filePathDel);
                    }

                    string UploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
                    FileName = Path.GetFileName(vm.Photo.FileName);
                    string filePath = Path.Combine(UploadsFolder, FileName);
                    using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        vm.Photo.CopyTo(fileStream);
                    }


                    product.ProductImagePath = FileName;
                }

                if (vm.Photo == null)
                {
                    if (vm.ExistingPhotoPath != null)
                    {
                        product.ProductImagePath = vm.ExistingPhotoPath;
                    }
                    else
                    {
                        product.ProductImagePath = null;
                    }
                }


                _reposProduct.UpdateProduct(product);
                return(RedirectToAction("ProductList", "Home"));
            }
            return(RedirectToAction("AddProduct"));
        }
コード例 #3
0
 public IActionResult AddProduct(ProductInputVM vm)
 {
     if (ModelState.IsValid)
     {
         string uniqueFileName = null;
         if (vm.Photo != null)
         {
             string UploadsFolder = Path.Combine(_hostingEnvironment.WebRootPath, "images");
             uniqueFileName = Path.GetFileName(vm.Photo.FileName);
             string filePath = Path.Combine(UploadsFolder, uniqueFileName);
             using (var fileStream = new FileStream(filePath, FileMode.Create))
             {
                 vm.Photo.CopyTo(fileStream);
             }
         }
         Product product = new Product
         {
             Name             = vm.Name,
             Price            = vm.Price,
             Quantity         = vm.Quantity,
             BrandId          = vm.BrandId.Value,
             CategoryId       = vm.CategoryId.Value,
             ProductImagePath = uniqueFileName
         };
         _reposProduct.AddProduct(product);
         var listOfSize = _reposSize.GetSizes();
         for (var s = 0; s < listOfSize.Count(); s++)
         {
             ProductSize productSize = new ProductSize
             {
                 ProductId = product.Id,
                 SizeId    = listOfSize[s].Id
             };
             _reposProductSize.Add(productSize);
         }
         return(RedirectToAction("ProductList", "Home"));
     }
     return(View(vm));
     //return RedirectToAction("AddProduct", "Admin");
 }
コード例 #4
0
        public IActionResult UpdateProduct(int Id)
        {
            ProductInputVM vm = new ProductInputVM();

            var product = _reposProduct.GetProduct(Id);

            var brands = _reposBrand.GetBrands();

            var categories = _reposCategory.GetCategories();

            vm.Id                = product.Id;
            vm.Name              = product.Name;
            vm.Price             = product.Price;
            vm.Quantity          = product.Quantity;
            vm.BrandId           = product.BrandId;
            vm.CategoryId        = product.CategoryId;
            vm.ExistingPhotoPath = product.ProductImagePath;
            vm.Brands            = brands.Select(b => new SelectListItem(b.Name, b.Id.ToString())).ToList();
            vm.Categories        = categories.Select(c => new SelectListItem(c.Name, c.Id.ToString())).ToList();

            return(View(vm));
        }