示例#1
0
        public IActionResult Upload(IFormFile file)
        {
            if (_csvHelper.CsvIsValid(file))
            {
                _productRepository.DeleteAllProducts();

                IEnumerable <Product> productsToAdd = _csvHelper.ReadFromCsv(file);

                _productRepository.AddProducts(productsToAdd);
                _productRepository.SaveChanges();

                TempData["Success"] = $"{productsToAdd.Count()} products successfully imported";
            }
            else
            {
                TempData["Error"] = "Please choose a valid .csv file";
            }

            return(RedirectToAction("Index"));
        }