コード例 #1
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");
 }