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)); }
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)); }
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")); }
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()); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(Page()); } _context.Products.Add(Product); await _context.SaveChangesAsync(); return(RedirectToPage("./Index")); }