//========================================================== //========================================================== // GET: AdminPanel/Products/Create public ActionResult Create() { ViewProductCreate model = new ViewProductCreate(); // все категории model.CategoriesAll = db.ShopCategories.OrderBy(c => c.Name).ToList(); // список вкусов List <ShopProductsTaste> tastes = db.ShopProductsTastes.OrderBy(c => c.Name).ToList(); model.TasteList = new SelectList(tastes, "Id", "Name"); // список брэндов List <ShopProductsBrand> brands = db.ShopProductsBrands.OrderBy(c => c.Name).ToList(); model.BrandList = new SelectList(brands, "Id", "Name"); return(View(model)); }
public ActionResult Create(ViewProductCreate model) { for (int i = 0; i < model.SelectedCategoriesId.Length; i++) { ShopCategory category = db.ShopCategories.Find(model.SelectedCategoriesId[i]); model.CategoriesSelected.Add(category); } if (ModelState.IsValid) { ShopProduct product = new ShopProduct { Name = model.Name, Alias = Translit.TranslitString(model.Name), Description = model.Description, VendorCode = model.VendorCode, Weight = model.Weight.Value, Proteins = model.Proteins.Value, Fats = model.Fats.Value, Carbohydrates = model.Carbohydrates.Value, Kcal = model.Kcal.Value }; //брэнд if (model.SelectedBrandId.HasValue) { product.ShopProductsBrand = db.ShopProductsBrands.Find(model.SelectedBrandId); } // вкус if (model.SelectedTasteId.HasValue) { product.ShopProductsTaste = db.ShopProductsTastes.Find(model.SelectedTasteId); } // катгории product.ShopCategories = model.CategoriesSelected; if (model.PortionsWeight.HasValue) { product.PortionsWeight = model.PortionsWeight.Value; } if (model.PortionsCount.HasValue) { product.PortionsCount = model.PortionsCount.Value; } product.DateCreation = DateTime.Now; if (model.Price != null) { ShopProductsPrice price = new ShopProductsPrice { Price = decimal.Parse(model.Price), CurrentPrice = true, DateSet = product.DateCreation, }; product.ShopProductsPrices.Add(price); } //сохранение фото товра if (model.PhotoFile != null) { string fileName = Guid.NewGuid().ToString(); string dirPath = HttpContext.Server.MapPath("~/Content/Images/Shop/Products"); product.PhotoName = Image.Save( model.PhotoFile, dirPath, null, fileName); } db.ShopProducts.Add(product); db.SaveChanges(); return(RedirectToAction("Index")); } // все категории model.CategoriesAll = db.ShopCategories.OrderBy(c => c.Name).ToList(); // список вкусов List <ShopProductsTaste> tastes = db.ShopProductsTastes.OrderBy(c => c.Name).ToList(); model.TasteList = new SelectList(tastes, "Id", "Name"); // список брэндов List <ShopProductsBrand> brands = db.ShopProductsBrands.OrderBy(c => c.Name).ToList(); model.BrandList = new SelectList(brands, "Id", "Name"); return(View(model)); }