public async Task <IActionResult> Product(ProductView product, IFormFile?formFile) { ViewBag.Brands = new List <Brand>(); ViewBag.Sections = new List <Section>(); if (ModelState.IsValid) { if (product.Id != -1) { var productDB = _sqlProductData.GetProductById(product.Id); productDB.Name = product.Name; productDB.Order = product.Order; productDB.Price = product.Price; productDB.ImageUrl = formFile?.FileName ?? product.ImageUrl; productDB.BrandId = product.BrandId; productDB.SectionId = product.SectionId; } else { var productNew = new Product() { Name = product.Name, Order = product.Order, Price = product.Price, ImageUrl = formFile?.FileName ?? product.ImageUrl, BrandId = product.BrandId, SectionId = product.SectionId }; _sqlProductData.CreateProduct(productNew); } if (formFile != null) { var path = "/images/shop/" + formFile.FileName; using (var fs = new FileStream(_environment.WebRootPath + path, FileMode.Create)) { await formFile.CopyToAsync(fs); } } _sqlProductData.SaveDB(); return(RedirectToAction("Products", "ShopElemet", new { area = "Admin" })); } var brands = _sqlProductData.GetBrands(); ViewBag.Brands = new SelectList(brands, "Id", "Name"); var sections = _sqlProductData.GetSections(); ViewBag.Sections = new SelectList(sections, "Id", "Name"); return(View(product)); }
public IActionResult Product(int?id) { var productDB = id != null?_sqlProductData.GetProductById((int)id) : new Product() { Id = -1 }; var productView = new ProductView() { Id = productDB.Id, Name = productDB.Name, Order = productDB.Order, Price = productDB.Price, ImageUrl = productDB.ImageUrl, BrandId = (int)(productDB.BrandId != null ? productDB.BrandId : null), Brand = new BrandSectionView() { Id = productDB.Brand.Id, Name = productDB.Brand.Name, Order = productDB.Brand.Order }, SectionId = productDB.SectionId, Section = new BrandSectionView() { Id = productDB.Section.Id, Name = productDB.Section.Name, Order = productDB.Section.Order } }; var brands = _sqlProductData.GetBrands(); ViewBag.Brands = new SelectList(brands, "Id", "Name", productView.BrandId); var sections = _sqlProductData.GetSections(); ViewBag.Sections = new SelectList(sections, "Id", "Name", productView.SectionId); return(View(productView)); }