Reputation: 29
When I try to delete a Page(table 2), images about the page will be deleted from Image's folder!!!!
BUT When I try to delete a PageGroup(table 1) that includes many Pages, images about these Pages (that are already saved in image's folder(Pages, table 2)) will not be deleted! And they are still remain in the folder!
Can u help me plz, how to write a query to solve this problem???
Thanks!
public ActionResult DeleteConfirmed(int id)
{
var d = db.PageGroups.Find(id);
db.PageGroups.Remove(d);
db.SaveChanges();
return RedirectToAction("Index");
}
Upvotes: 0
Views: 121
Reputation: 29
This is how to solve this problem : ( If someone else has the same problem! )
public ActionResult DeleteConfirmed(int id)
{
Var a = db. Pages.Where(p => p.PageGroup == id).ToList();
foreach (var item in a)
{
System.Io.File.Delete(Server.MapPath("/images/" + item.ImageName));
}
var d = db.PageGroups.Find(id);
db.PageGroups.Remove(d);
db.SaveChanges();
return RedirectToAction("Index");
}
Upvotes: 2