public ActionResult DeleteConfirmed(int id) { VesselPhoto vesselphoto = db.VesselPhotos.Find(id); // get the filename var fileName = Path.GetFileName(vesselphoto.Image); // get the path to the file var path = Path.Combine(Server.MapPath("~/Images"), fileName); // delete the file try { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } } catch (Exception) { ModelState.AddModelError("uploadError", "Can't delete the file."); } db.VesselPhotos.Remove(vesselphoto); db.SaveChanges(); //return RedirectToAction("Index"); var redirectURL = "../../Vessel/Edit/" + vesselphoto.VesselID + "#images"; return(Redirect(redirectURL)); }
// GET: /VesselPhoto/DetailsForGallery/5 public ActionResult DetailsForGallery(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } VesselPhoto vesselphoto = db.VesselPhotos.Find(id); if (vesselphoto == null) { return(HttpNotFound()); } return(View(vesselphoto)); }
// GET: /VesselPhoto/Edit/5 public ActionResult Edit(int?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } VesselPhoto vesselphoto = db.VesselPhotos.Find(id); if (vesselphoto == null) { return(HttpNotFound()); } ViewBag.PhotoSourceID = new SelectList(db.PhotoSources.OrderBy(s => s.Source), "ID", "Source", vesselphoto.PhotoSourceID); ViewBag.PhotoTypeID = new SelectList(db.PhotoTypes.OrderBy(s => s.PhotoType1), "ID", "PhotoType1", vesselphoto.PhotoTypeID); // Set this to false by default ViewBag.ShowImageExistsError = false; return(View(vesselphoto)); }
public ActionResult Create([Bind(Include = "ID,VesselID,Image,Thumb,Caption,PhotoSourceID,PhotoTypeID,DateOfPhoto")] VesselPhoto vesselphoto, HttpPostedFileBase file) { var redirectURL = "../Vessel/Edit/" + vesselphoto.VesselID + "#images"; // If the page is reloaded with an error message from below, the VesselID is lost in the querystring // so save the VesselID in the ViewBag to use in those cases ViewBag.VesselID = vesselphoto.VesselID; // The ViewBag with the dropdown lists must also be recreated ViewBag.PhotoSourceID = new SelectList(db.PhotoSources.OrderBy(s => s.Source), "ID", "Source"); ViewBag.PhotoTypeID = new SelectList(db.PhotoTypes.OrderBy(s => s.PhotoType1), "ID", "PhotoType1"); // if file's content length is zero or no files submitted if (Request.Files.Count != 1 || Request.Files[0].ContentLength == 0) { ModelState.AddModelError("uploadError", "Please select an image to upload."); return(View(vesselphoto)); } // check the file size (max 4 Mb) if (Request.Files[0].ContentLength > 1024 * 1024 * 4) { ModelState.AddModelError("uploadError", "File size can't exceed 4 MB"); return(View(vesselphoto)); } // check file extension string extension = Path.GetExtension(Request.Files[0].FileName).ToLower(); if (extension != ".jpg" && extension != ".jpeg" && extension != ".gif" && extension != ".png") { ModelState.AddModelError("uploadError", "Supported file extensions: jpg, jpeg, gif, png"); return(View(vesselphoto)); } // extract only the filename var fileName = Path.GetFileName(file.FileName); // store the file inside ~/Images folder var path = Path.Combine(Server.MapPath("~/Images"), fileName); try { if (!System.IO.File.Exists(path)) { Request.Files[0].SaveAs(path); //System.IO.File.Delete(path); } else { ModelState.AddModelError("uploadError", "An image with that name already exists. Please rename your image."); return(View(vesselphoto)); } } catch (Exception) { ModelState.AddModelError("uploadError", "Can't save file to disk"); } if (ModelState.IsValid) { vesselphoto.Image = file.FileName; db.VesselPhotos.Add(vesselphoto); db.SaveChanges(); //return RedirectToAction("Index"); return(Redirect(redirectURL)); } //ViewBag.AttractionID = new SelectList(db.Attractions, "ID", "AttractionName", attractionphoto.AttractionID); //return View(attractionphoto); return(Redirect(redirectURL)); }