public ActionResult AlbumPhotoDelete(int id) { if (Session["Admin_Id"] == null) { return(RedirectToAction("Index")); } try { PhotoAlbumDetail pad = db.PhotoAlbumDetails.Find(id); if (pad != null) { try { System.IO.File.Delete(Server.MapPath("~/Uploaded/" + pad.Photo)); } catch { } db.PhotoAlbumDetails.Remove(pad); int i = db.SaveChanges(); if (i > 0) { return(RedirectToAction("AlbumDetail", new { id = Session["PhotoAlbum_Id"] })); } else { ViewBag.error = "Failed to delete photo"; } } } catch { ViewBag.error = "Technical Error"; } return(View()); }
public ActionResult AddPhotos(int id = 0) { if (Session["Admin_Id"] == null) { return(RedirectToAction("Index")); } try { PhotoAlbumDetail pad = new PhotoAlbumDetail(); pad.Photo = ""; pad.PhotoAlbum_Id = Convert.ToInt32(Session["PhotoAlbum_Id"]); HttpPostedFileBase file = Request.Files["url"]; if (file.FileName.Length > 0) { string filename = "Image_" + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss_") + file.FileName; string ext = System.IO.Path.GetExtension(filename); if (ext.ToLower() == ".jpg" || ext.ToLower() == "jpeg") { file.SaveAs(Server.MapPath("~/Uploaded/" + filename)); pad.Photo = filename; db.PhotoAlbumDetails.Add(pad); int i = db.SaveChanges(); if (i > 0) { return(RedirectToAction("AlbumDetail", new { id = Session["PhotoAlbum_Id"] })); } else { ViewBag.error = "Filed to upload photo"; } } else { ViewBag.error = "Image format is not valid | It must be .jpg or .jpeg"; } } } catch { ViewBag.error = "Technical Error"; } return(View()); }