//public void AddJewelry(AddJewelry jewelry, ImageHelper image) public void AddJewelry(AddJewelry jewelry) { using (var _context = new SmyckenContext()) { var image = new Image() { FileName = jewelry.PictureFileName, Url = jewelry.PictureURL, News = false, Description = "", Category = jewelry.Category }; _context.Images.Add(image); _context.SaveChanges(); var newJewelry = _context.Jewelries.Add(new Jewelry() { Name = jewelry.Name, Price = jewelry.Price, Quantity = jewelry.Quantity, Description = jewelry.Description, Category = jewelry.Category, Visibility = true, Image = image }); _context.Jewelries.Add(newJewelry); _context.SaveChanges(); } }
public ActionResult AddJewelry(HttpPostedFileBase file, AddJewelry jewelry) { if (file != null) { string subPath = picturePath + jewelry.Name.Replace(" ", "_"); //jewelry.PictureFileName = DateTime.Now.ToString("yyyyMMdd-Hmmss") + "_" + file.FileName; jewelry.PictureFileName = file.FileName; jewelry.PictureURL = subPath + "/"; if (ModelState.IsValid) { bool exists = Directory.Exists(Server.MapPath(subPath)); if (!exists) Directory.CreateDirectory(Server.MapPath(subPath)); if (file != null && file.ContentLength > 0) try { string path = Path.Combine(Server.MapPath("/Images/" + jewelry.Name.Replace(" ", "_") + "/"), Path.GetFileName(jewelry.PictureFileName)); file.SaveAs(path); ViewBag.Message = "File uploaded successfully"; _smyckeRepoCreate.AddJewelry(jewelry); //return RedirectToAction("ShowSelectedBlog", "Horse", new { horseId = blogPost.HorseId }); } catch (Exception ex) { ViewBag.Message = "ERROR:" + ex.Message.ToString(); } } } else { if (ModelState.IsValid) { _smyckeRepoCreate.AddJewelry(jewelry); //return RedirectToAction("ShowSelectedBlog", "Horse", new { horseId = jewelry.HorseId }); } } return RedirectToAction("Products", "Admin"); }
public ActionResult AddJewelry() { var jewelry = new AddJewelry(); return View(jewelry); }