コード例 #1
0
        public async Task <IActionResult> Edit(int?id, string[] selectedSubCategories, IFormFile photoPath)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var productToUpdate = await _context.Products
                                  .Include(i => i.ProductSubcategories)
                                  .ThenInclude(i => i.SubCategory)

                                  .SingleOrDefaultAsync(m => m.Id == id);

            if (photoPath != null)
            {
                //1 -) create directory
                string uploadPath = Path.Combine(_environment.WebRootPath, "Uploads\\Products");
                //uploadPath = Path.Combine(uploadPath, product.Name);
                Directory.CreateDirectory(Path.Combine(uploadPath, productToUpdate.Name));
                //2 -) get the file name
                string FileName = Path.GetFileName(photoPath.FileName);

                //3 -)
                using (FileStream fs = new FileStream(Path.Combine(uploadPath, productToUpdate.Name, FileName), FileMode.Create))
                {
                    photoPath.CopyTo(fs);
                }
                //4 -) change the pack photoPath
                productToUpdate.PhotoPath = Path.Combine(productToUpdate.Name, FileName);
            }
            else
            {
                productToUpdate.PhotoPath = productToUpdate.PhotoPath;
            }

            if (await TryUpdateModelAsync <Product>(
                    productToUpdate,
                    "",
                    i => i.Name, i => i.Description, i => i.Category))
            {
                UpdateProductsSubCategories(selectedSubCategories, productToUpdate);


                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateException /* ex */)
                {
                    //Log the error (uncomment ex variable name and write a log.)
                    ModelState.AddModelError("", "Unable to save changes. " +
                                             "Try again, and if the problem persists, " +
                                             "see your system administrator.");
                }
                return(RedirectToAction(nameof(Index)));
            }
            UpdateProductsSubCategories(selectedSubCategories, productToUpdate);
            PopulateAssignedSubCategoryData(productToUpdate);
            return(View(productToUpdate));
        }
コード例 #2
0
        public async Task <IActionResult> Create([Bind("Id,Name")] SubCategory subCategory)
        {
            if (ModelState.IsValid)
            {
                _context.Add(subCategory);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(subCategory));
        }
コード例 #3
0
        public async Task <IActionResult> OnPostAsync(int?productId)
        {
            if (productId == null)
            {
                return(RedirectToPage("./NotFound"));
            }

            Product product = await _context.Products
                              .Include(i => i.ProductSubcategories)
                              .SingleAsync(i => i.Id == productId);

            //Product = await _context.Products.FindAsync(id);

            if (product != null)
            {
                _context.Products.Remove(product);
                await _context.SaveChangesAsync();
            }
            else
            {
                return(RedirectToPage("./NotFound"));
            }

            return(RedirectToPage("./Index"));
        }
コード例 #4
0
        public async Task <IActionResult> OnPostAsync(int?productId, string[] selectedSubcategories)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToPage("./NotFound"));
            }

            _context.Attach(Product).State = EntityState.Modified;

            var productToUpdate = await _context.Products

                                  .Include(i => i.ProductSubcategories)
                                  .ThenInclude(i => i.SubCategory)
                                  .FirstOrDefaultAsync(s => s.Id == productId);



            if (await TryUpdateModelAsync <Product>(
                    productToUpdate,
                    "Product",
                    i => i.Name, i => i.Description,
                    i => i.Category))
            {
                UpdateProductSubcategory(_context, selectedSubcategories, productToUpdate);
                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }
            UpdateProductSubcategory(_context, selectedSubcategories, productToUpdate);
            PopulateProductSubcategoryData(_context, productToUpdate);
            return(Page());
        }
コード例 #5
0
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            _context.Products.Add(Product);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }