public ActionResult DeleteImage(int id) { Tbl_PerfumeImages image = db.Tbl_PerfumeImages.Where(a => a.PerfumeImages_ID == id).SingleOrDefault(); int perfumeID = (int)image.PerfumeImages_Perfume_ID; db.Tbl_PerfumeImages.Remove(image); db.SaveChanges(); return(RedirectToAction("EditPerfume", "DashboardPerfume", new { perfumeID = perfumeID })); }
public bool AddNewImages(IEnumerable <HttpPostedFileBase> files, int perfumeID) { List <Tbl_PerfumeImages> p_images = new List <Tbl_PerfumeImages>(); if (files.Count() > 0) { foreach (var file in files) { var uploadFiles = file; Random rnd = new Random(); if (uploadFiles != null && uploadFiles.ContentLength > 0) { if (!validImageTypes.Contains(uploadFiles.ContentType)) { Message = "invalid image format."; log.addLog(Message, "EditPerfume", "DashboardPerfume", logStatus.EventLog); ViewBag.Error = "عکس آپلود شده باید از نوع jpg ویا png باشد."; return(false); } //add new image Tbl_PerfumeImages images = new Tbl_PerfumeImages(); var fileName = rnd.Next().ToString() + ".jpg"; images.PerfumeImages_ImageIndex = fileName; images.PerfumeImages_Perfume_ID = perfumeID; var path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("/Uploads/PerfumeImages/"), fileName); //var path = Path.Combine(Server.MapPath("/Uploads/PerfumeImages/"), fileName); uploadFiles.SaveAs(path); p_images.Add(images); Message = "Image save successfully for Perfumes with id " + perfumeID + "."; log.addLog(Message, "EditPerfume", "DashboardPerfume", logStatus.EventLog); } } } db.Tbl_PerfumeImages.AddRange(p_images); db.SaveChanges(); return(true); }
public ActionResult AddPerfume(Tbl_Perfume perfume, int?spring, int?summer, int?autumn, int?winter, IEnumerable <HttpPostedFileBase> files) { String Message = ""; if (Session["UserName"] == null) { Message = "Access denied. need login."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.EventLog); return(RedirectToAction("Index", "Account", new { returnUrl = "/DashboardPerfume/Index" })); } else if (Session["RoleName"].ToString() == "Admin") { try { var validImageTypes = new string[] { "image/gif", "image/jpg", "image/jpeg", "image/pjpeg", "image/png" }; InitDropdownLists(); Tbl_Perfume p = new Tbl_Perfume(); p.Perfume_Brand_ID = perfume.Perfume_Brand_ID; p.Perfume_Gender = perfume.Perfume_Gender; p.Perfume_Country = perfume.Perfume_Country; p.Perfume_Name = perfume.Perfume_Name; p.Perfume_Notes = perfume.Perfume_Notes; p.Perfume_OlfactionGroups = perfume.Perfume_OlfactionGroups; p.Perfume_Perfumer = perfume.Perfume_Perfumer; p.Perfume_Description = perfume.Perfume_Description; p.Perfume_SpecialOffer = perfume.Perfume_SpecialOffer; p.Perfume_TemperOfPerfume = perfume.Perfume_TemperOfPerfume; p.Perfume_Type_ID = perfume.Perfume_Type_ID; if (perfume.Perfume_ImageIndex != null && perfume.Perfume_ImageIndex != "") { var uploadFiles = Request.Files[0]; Random rnd = new Random(); if (!validImageTypes.Contains(uploadFiles.ContentType)) { Message = "invalid image format."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.EventLog); ViewBag.Error = "عکس آپلود شده باید از نوع jpg ویا png باشد."; return(View()); } else if (uploadFiles != null && uploadFiles.ContentLength > 0) { var fileName = rnd.Next().ToString() + ".jpg"; p.Perfume_ImageIndex = fileName; var path = Path.Combine(System.Web.HttpContext.Current.Server.MapPath("/Uploads/PerfumeImages/"), fileName); uploadFiles.SaveAs(path); Message = "Image save successfully for Perfumes with title " + p.Perfume_Name + "."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.EventLog); //return View(); db.Tbl_Perfume.Add(p); if (Convert.ToBoolean(db.SaveChanges() > 0)) { //////add season//////// List <Tbl_PerfumeSeason> seasons = new List <Tbl_PerfumeSeason>(); if (spring == 1) { Tbl_PerfumeSeason ps = new Tbl_PerfumeSeason(); int springID = db.Tbl_Season.Where(a => a.Season_Title == "مناسب فصل بهار").SingleOrDefault().Season_ID; ps.PerfumeSeason_Season_ID = springID; ps.PerfumeSeason_Perfume_ID = p.Perfume_ID; seasons.Add(ps); } if (summer == 1) { Tbl_PerfumeSeason ps = new Tbl_PerfumeSeason(); int summerID = db.Tbl_Season.Where(a => a.Season_Title == "مناسب فصل تابستان").SingleOrDefault().Season_ID; ps.PerfumeSeason_Season_ID = summerID; ps.PerfumeSeason_Perfume_ID = p.Perfume_ID; seasons.Add(ps); } if (autumn == 1) { Tbl_PerfumeSeason ps = new Tbl_PerfumeSeason(); int autumnID = db.Tbl_Season.Where(a => a.Season_Title == "مناسب فصل پاییز").SingleOrDefault().Season_ID; ps.PerfumeSeason_Season_ID = autumnID; ps.PerfumeSeason_Perfume_ID = p.Perfume_ID; seasons.Add(ps); } if (winter == 1) { Tbl_PerfumeSeason ps = new Tbl_PerfumeSeason(); int winterID = db.Tbl_Season.Where(a => a.Season_Title == "مناسب فصل زمستان").SingleOrDefault().Season_ID; ps.PerfumeSeason_Season_ID = winterID; ps.PerfumeSeason_Perfume_ID = p.Perfume_ID; seasons.Add(ps); } db.Tbl_PerfumeSeason.AddRange(seasons); if (Convert.ToBoolean(db.SaveChanges() > 0)) { List <Tbl_PerfumeImages> images = new List <Tbl_PerfumeImages>(); Tbl_PerfumeImages imageIndexes = new Tbl_PerfumeImages(); foreach (var file in files) { if (file.ContentLength > 0) { var fileName1 = rnd.Next().ToString() + ".jpg"; var path1 = Path.Combine(Server.MapPath("/Uploads/PerfumeImages/"), fileName1); file.SaveAs(path1); imageIndexes = new Tbl_PerfumeImages(); imageIndexes.PerfumeImages_Perfume_ID = p.Perfume_ID; imageIndexes.PerfumeImages_ImageIndex = fileName1; images.Add(imageIndexes); } } db.Tbl_PerfumeImages.AddRange(images); if (Convert.ToBoolean(db.SaveChanges() > 0)) { Message = "Perfume with title " + perfume.Perfume_Name + " added successfully."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.EventLog); return(RedirectToAction("Index", "DashboardPerfume")); } else { db.Tbl_Perfume.Remove(p); db.SaveChanges(); Message = "added Perfume with title " + perfume.Perfume_Name + " failed."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.ErrorLog); ViewBag.Error = "محصول جدید ثبت نشد، لطفا دوباره تلاش کنید."; return(View()); } } else { db.Tbl_Perfume.Remove(p); db.SaveChanges(); Message = "added Perfume with title " + perfume.Perfume_Name + " failed."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.ErrorLog); ViewBag.Error = "محصول جدید ثبت نشد، لطفا دوباره تلاش کنید."; return(View()); } } else { Message = "added Perfume with title " + perfume.Perfume_Name + " failed."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.EventLog); ViewBag.Error = "محصول جدید ثبت نشد، لطفا دوباره تلاش کنید."; return(View()); } } else { Message = "Image save for Perfuem with title " + p.Perfume_Name + " failed."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.EventLog); ViewBag.EmptyImage = "آپلود عکس با خطا مواجه شده است."; return(View()); } } else { Message = "added Perfume with title " + perfume.Perfume_Name + " failed."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.EventLog); ViewBag.Error = "محصول جدید ثبت نشد، لطفا دوباره تلاش کنید."; return(View()); } } catch { Message = "added Perfume with title " + perfume.Perfume_Name + " failed."; log.addLog(Message, "AddPerfume", "DashboardPerfume", logStatus.ErrorLog); ViewBag.Error = "محصول جدید ثبت نشد، لطفا دوباره تلاش کنید."; return(View()); } } else { Message = "You do not have access to this page."; log.addLog(Message, "AddUser", "DashboardUser", logStatus.EventLog); return(RedirectToAction("Error404", "Home")); } }