I have a delete button that calls a javascript function onclick. The function function fires, gets id and displays in Alert, the tries to use window.location.href = "/Home/DeletePhoto/photoId=" + id; to call an ActionResult in the HomeController.
button in modal popup:
<button class="title" id="btnDelete" onclick="fDoTheDelete();">Delete</button>
javascript function at the bottom of the View:
function fDoTheDelete()
{
alert("in the delete function: " + document.getElementById('h3Title').innerHTML);
var id = document.getElementById('h3Title').innerHTML;
window.location.href = "http://localhost:50061/Home/DeletePhoto/photoId=" + id;
}
(if I Ctrl + Click on the URL below in the javascript code, it runs the ActionResult as required)
window.location.href = "http://localhost:50061/Home/DeletePhoto/photoId=" + id;
Here is the ActionResult. I want to delete a photo and it's record and redirect to Home/Index which will display photos without the deleted one.
public ActionResult DeletePhoto(int photoId)
{
//GET FILENAME FROM ID
string photoName = (from p in db.Photos
where p.PhotoId == photoId
select p.Description).FirstOrDefault();
//DELETE FILE
string fullPath = "/GalleryImages /" + photoName;
if (System.IO.File.Exists(fullPath))
{
System.IO.File.Delete(fullPath);
}
//DELETE RECORD OF PHOTO
Photo photo = db.Photos.Find(photoId);
db.Photos.Remove(photo);
db.SaveChanges();
return RedirectToAction("Index");
}
On button click, Alerts show that I am inside javascript function, and the id has been passed, but the photo Modal window just closes and displays same Index showing the thumbnail of the image still exists.
EDIT: I came back to edit and add the information that I am using This image gallery which was very easy to configure but made it difficult to pass additional stuff between the thumbnail/index display and the Modal lightbox popup. That is why I am having so much trouble with a simple call to an ActionResult. There is lots of js/jquery/blueimp code that is difficult to hack. thanks for everyone that made an effort to help me and I hope someone else can use the Image Gallery too.