public ActionResult DeleteCategory(int id)
{
CategoryManager manager = new CategoryManager();
manager.DeleteCategory(id);
TempData["IsDeleted"] = true;
return RedirectToAction("CategoriesList");
}
public ActionResult CategoriesList()
{
List<CategoryModel> model = new CategoryManager().GetAll();
return View(model);
}
public void DeleteCategory(int categoryId)
{
using (AsoEntities context = new AsoEntities())
{
var categoryToDelete = (from c in context.Categories
where c.Id == categoryId
select c).SingleOrDefault();
if (categoryToDelete == null)
return;
context.Categories.DeleteObject(categoryToDelete);
context.SaveChanges();
}
}
Javascript:
$(document).ready(function () {
// Dialog
$('.delete-link').click(function () {
deleteLinkObj = $(this); //for future use
$('#delete-dialog').dialog('open');
return false; // prevents the default behaviour
});
$('#delete-dialog').dialog({
autoOpen: false, width: 400, resizable: false, modal: true, //Dialog options
buttons: {
"Da": function () {
$.post(deleteLinkObj[0].href, function (data) { //Post to action
if (data == '<%= Boolean.TrueString %>') {
deleteLinkObj.closest("tr").hide('fast'); //Hide Row
//(optional) Display Confirmation
}
else {
//(optional) Display Error
}
});
$(this).dialog("close");
},
"Ne": function () {
$(this).dialog("close");
}
}
});
});
When I delete an article I am taken back to CategoriesList; but the page is not reloaded if I am already on CategoriesList. How can I make it so that the page will be re-loaded and the data will be refreshed?
Edit:
If I remove the Javascript then it starts working. Where is the problem in the Javscript?
You are using jquery to delete the item asynchronously which will mean that the page is not reloaded.
Your code has this line: if (data == ‘<%= Boolean.TrueString %>’) { but in the server side method is not returning a bool so the above line will never equal true and so deleteLinkObj.closest(“tr”).hide(‘fast’); //Hide Row is never called.