public async Task <ActionResult> DeleteGallery(int GalleryID) { if (Session["user"] != null) { List <tblPhoto> tblPhotos = db.tblPhotos.Where(gallery => gallery.GalleryID.Equals(GalleryID)).ToList(); foreach (var file in tblPhotos) { string fullPath = Request.MapPath(fileLocation + file.Url); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); tblPhoto tblPhoto = db.tblPhotos.Find(file.PhotoID); db.tblPhotos.Remove(tblPhoto); } } tblGallery tblGallery = await db.tblGalleries.FindAsync(GalleryID); db.tblGalleries.Remove(tblGallery); await db.SaveChangesAsync(); return(RedirectToAction("Portfolio", "Home")); } else { return(RedirectToAction("Portfolio", "Home")); } }
public ActionResult deletePhoto(int PhotoID) { if (Session["user"] != null) { var findPhoto = db.tblPhotos.Find(PhotoID); var fileName = findPhoto.Url; int GalleryID = findPhoto.GalleryID; string fullPath = Request.MapPath(fileLocation + fileName); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); tblPhoto tblPhotos = db.tblPhotos.Find(PhotoID); var photoID = tblPhotos.PhotoID; db.tblPhotos.Remove(tblPhotos); } db.SaveChanges(); return(RedirectToAction("PhotoGallery", "Portfolio", new { GalleryID = GalleryID })); } return(RedirectToAction("Portfolio", "Home")); }
public ActionResult AddOrEdit(int id = 0) { if (id == 0) { return(View("AddOrEdit")); } else { tblHostel th = db.tblHostels.Where(x => x.HostelId == id).FirstOrDefault(); HostelViewModel hm = new HostelViewModel(); tblPhoto photo = new tblPhoto(); hm.HostelName = th.HostelName; hm.Description = th.Description; hm.Address = th.Address; hm.Phone = th.Phone; hm.Star = th.Star; hm.HostelId = id; hm.Capacity = th.Capacity; hm.DescriptionNext = th.DescriptionNext; hm.Facilites = th.Facilites; hm.Email = th.Email; hm.MapLocation = th.MapLocation; List <string> sth = new List <string>(); foreach (var item in db.tblPhotoes.ToList()) { if (item.HostelId == id) { sth.Add(item.Photo); } } hm.Photo = th.Photo; if (sth.Count != 0) { hm.Photo1 = sth[0]; hm.Photo2 = sth[1]; hm.Photo3 = sth[2]; } foreach (var item in db.tblPrices.ToList()) { List <tblRoom> to = db.tblRooms.Where(x => x.PriceId == item.PriceId).ToList(); foreach (var room in to) { if (room.RoomName == "One Sitter" && room.HostelId == id) { hm.OnesitterPrice = room.tblPrice.Price; } if (room.RoomName == "Two Sitter" && room.HostelId == id) { hm.TwositterPrice = room.tblPrice.Price; } if (room.RoomName == "Three Sitter" && room.HostelId == id) { hm.ThreesitterPrice = room.tblPrice.Price; } } } return(View("AddOrEdit", hm)); } }
public ActionResult uploadPhotos(int galleryID) { if (Session["user"] != null) { /******* * upload script supplied from the following web site : * http://www.c-sharpcorner.com/article/asp-net-mvc-drag-drop-multiple-image-upload/ ******/ bool isSavedSuccessfully = true; bool isAddedSucessfully = true; string fName = ""; string fNameWithoutExtension = ""; try { foreach (string fileName in Request.Files) { HttpPostedFileBase file = Request.Files[fileName]; fName = file.FileName; if (file != null && file.ContentLength > 0) { var fileName1 = Path.GetFileName(file.FileName); fNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FileName); var path = Path.Combine(Server.MapPath(fileLocation)); string pathString = System.IO.Path.Combine(path.ToString()); bool isExists = System.IO.Directory.Exists(pathString); if (!isExists) { System.IO.Directory.CreateDirectory(pathString); } var uploadpath = string.Format("{0}\\{1}", pathString, file.FileName); file.SaveAs(uploadpath); try { tblPhoto newPhoto = new tblPhoto(); newPhoto.PhotoName = fNameWithoutExtension; newPhoto.Description = fNameWithoutExtension; newPhoto.Url = fName; newPhoto.DateStamp = DateTime.Now; newPhoto.GalleryID = galleryID; db.tblPhotos.Add(newPhoto); db.SaveChangesAsync(); } catch (Exception) { isAddedSucessfully = false; } if (isAddedSucessfully) { return(Json(new { Message = fName })); } else { return(Json(new { Message = "Error in adding to the database" })); } } } } catch (Exception) { isSavedSuccessfully = false; } if (isSavedSuccessfully) { return(Json(new { Message = fName })); } else { return(Json(new { Message = "Error in saving file" })); } } else { RedirectToAction("Portfolio", "Home"); } return(View()); }
public ActionResult AddOrEdit(HostelViewModel hm) { if (ModelState.IsValid) { if (hm.HostelId == 0) { HttpPostedFileBase fup = Request.Files["Photo"]; HttpPostedFileBase one = Request.Files["Photo1"]; HttpPostedFileBase two = Request.Files["Photo2"]; HttpPostedFileBase three = Request.Files["Photo3"]; //Adding New One tblHostel th = new tblHostel(); tblPhoto photo = new tblPhoto(); th.HostelName = hm.HostelName; th.Description = hm.Description; th.Address = hm.Address; th.Phone = hm.Phone; th.Star = hm.Star; th.Capacity = hm.Capacity; th.DescriptionNext = hm.DescriptionNext; th.Facilites = hm.Facilites; th.Photo = fup.FileName; fup.SaveAs(Path.Combine(Server.MapPath("~/Images/Hostel/"), fup.FileName)); th.Email = hm.Email; th.MapLocation = hm.MapLocation; foreach (var item in db.tblWardens.ToList()) { if (User.Identity.Name == item.UserName) { th.WardenId = item.WardenId; } } db.tblHostels.Add(th); db.SaveChanges(); tblRoom to1 = new tblRoom(); tblRoom to2 = new tblRoom(); tblRoom to3 = new tblRoom(); tblPrice pto1 = new tblPrice(); tblPrice pto2 = new tblPrice(); tblPrice pto3 = new tblPrice(); photo.Photo = one.FileName; one.SaveAs(Path.Combine(Server.MapPath("~/Images/Hostel/"), one.FileName)); photo.HostelId = th.HostelId; to1.RoomName = "One Sitter"; to1.HostelId = th.HostelId; pto1.Price = hm.OnesitterPrice; db.tblPhotoes.Add(photo); db.tblPrices.Add(pto1); db.SaveChanges(); to1.PriceId = pto1.PriceId; db.tblRooms.Add(to1); db.SaveChanges(); photo.Photo = two.FileName; two.SaveAs(Path.Combine(Server.MapPath("~/Images/Hostel/"), two.FileName)); photo.HostelId = th.HostelId; to2.RoomName = "Two Sitter"; to2.HostelId = th.HostelId; pto2.Price = hm.TwositterPrice; db.tblPhotoes.Add(photo); db.tblPrices.Add(pto2); db.SaveChanges(); to2.PriceId = pto2.PriceId; db.tblRooms.Add(to2); db.SaveChanges(); photo.Photo = three.FileName; three.SaveAs(Path.Combine(Server.MapPath("~/Images/Hostel/"), three.FileName)); photo.HostelId = th.HostelId; to3.RoomName = "Three Sitter"; to3.HostelId = th.HostelId; pto3.Price = hm.ThreesitterPrice; db.tblPhotoes.Add(photo); db.tblPrices.Add(pto3); db.SaveChanges(); to3.PriceId = pto3.PriceId; db.tblRooms.Add(to3); db.SaveChanges(); } else { HttpPostedFileBase fup = Request.Files["Photo"]; HttpPostedFileBase one = Request.Files["Photo1"]; HttpPostedFileBase two = Request.Files["Photo2"]; HttpPostedFileBase three = Request.Files["Photo3"]; tblHostel th = db.tblHostels.Where(x => x.HostelId == hm.HostelId).FirstOrDefault(); th.HostelName = hm.HostelName; th.Description = hm.Description; th.Address = hm.Address; th.Phone = hm.Phone; th.Star = hm.Star; th.Capacity = hm.Capacity; th.Email = hm.DescriptionNext; th.Facilites = hm.Facilites; th.Email = hm.Email; th.MapLocation = hm.MapLocation; int i = 0; foreach (var item in db.tblPhotoes.ToList()) { if (item.HostelId == hm.HostelId && i == 0) { if (one.ContentLength > 0) { System.IO.File.Delete(Path.Combine(Server.MapPath("~/Images/Hostel/"), item.Photo)); item.Photo = one.FileName; one.SaveAs(Path.Combine(Server.MapPath("~/Images/Hostel/"), one.FileName)); break; } if (two.ContentLength > 0) { System.IO.File.Delete(Path.Combine(Server.MapPath("~/Images/Hostel/"), item.Photo)); one.SaveAs(Path.Combine(Server.MapPath("~/Images/Hostel/"), two.FileName)); item.Photo = two.FileName; break; } if (three.ContentLength > 0) { System.IO.File.Delete(Path.Combine(Server.MapPath("~/Images/Hostel/"), item.Photo)); one.SaveAs(Path.Combine(Server.MapPath("~/Images/Hostel/"), three.FileName)); item.Photo = three.FileName; break; } } } db.SaveChanges(); List <tblRoom> to = db.tblRooms.Where(x => x.HostelId == hm.HostelId).ToList(); foreach (var item in to) { if (item.RoomName == "One Sitter") { item.tblPrice.Price = hm.OnesitterPrice; } if (item.RoomName == "Two Sitter") { item.tblPrice.Price = hm.TwositterPrice; } if (item.RoomName == "Three Sitter") { item.tblPrice.Price = hm.ThreesitterPrice; } } db.SaveChanges(); } } return(RedirectToAction("Index", "Opening")); }