public async Task <ActionResult> Create(ProductCreateEditVM model, HttpPostedFileBase upload, int[] sizes) { // сохраняем файл в папку Pictures в проекте string fileName = System.IO.Path.GetFileName(upload.FileName); upload.SaveAs(Server.MapPath("~/Content/Pictures/" + fileName)); string domain = Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port); var filePath = domain + "/Content/Pictures/" + fileName; model.Picture = new Models.PictureVM { Name = fileName, Path = filePath }; var productDTO = _automapper.Map <ProductDTO>(model); if (!await _productService.CreateAsync(productDTO, sizes)) { throw new Exception("Saving error"); } return(RedirectToAction("Index")); }
public async Task <ActionResult> Edit(ProductCreateEditVM model, HttpPostedFileBase upload, int[] sizes) { // сохраняем файл в папку Pictures в проекте string fileName = System.IO.Path.GetFileName(upload.FileName); upload.SaveAs(Server.MapPath("~/Content/Pictures/" + fileName)); string domain = Request.Url.Scheme + Uri.SchemeDelimiter + Request.Url.Host + (Request.Url.IsDefaultPort ? "" : ":" + Request.Url.Port); var filePath = domain + "/Content/Pictures/" + fileName; model.Picture = new Models.PictureVM { Name = fileName, Path = filePath }; var productDTO = _automapper.Map <ProductDTO>(model); if (!await _productService.CreateAsync(productDTO, sizes)) { throw new Exception("Saving error"); } return(RedirectToAction("Index")); //if (ModelState.IsValid) //{ // ProductModel curentProduct = db.Products.Where(p => p.Id == product.Id) // .Include(p => p.Sizes) // .FirstOrDefault(); // curentProduct.Sizes = new List<SizeModel>(); // foreach (var sizeID in sizes) // { // curentProduct.Sizes.Add(db.Sizes.Find(Convert.ToInt32(sizeID))); // } // if (uploadImage != null) // { // string fileName = System.IO.Path.GetFileName(uploadImage.FileName); // // сохраняем файл в папку Pictures в проекте // uploadImage.SaveAs(Server.MapPath("~/Pictures/" + fileName)); // product.PictureName = fileName; // product.PictureType = uploadImage.ContentType; // db.Entry(product).State = EntityState.Modified; // } // else // { // product.PictureName = curentProduct.PictureName; // db.Entry(curentProduct).CurrentValues.SetValues(product); // db.Entry(curentProduct).State = EntityState.Modified; // } // db.SaveChanges(); // return RedirectToAction("Index"); //} //ViewBag.SubtypeId = new SelectList(db.Subtypes, "Id", "Name", product.SubtypeId); //ViewBag.TradeMarkId = new SelectList(db.TradeMarks, "Id", "Name", product.TradeMarkId); //ViewBag.TypeId = new SelectList(db.Types, "Id", "Name", product.TypeId); //return View(product); }