如果客户端已经在页面上,MVC3 ActionResult不会重新加载页面
本文关键字:新加载 加载 MVC3 客户端 如果 ActionResult | 更新日期: 2023-09-27 18:15:21
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");
}
}
});
});
当我删除一篇文章时,我被带回到categoreslist;但是如果我已经在categoreslist上,页面不会重新加载。如何使页面重新加载并刷新数据?
编辑:如果我删除Javascript,然后它开始工作。javascript中的问题在哪里?
您正在使用jquery异步删除项目,这将意味着页面不会重新加载。
你的代码有这样一行:if (data == '<%= Boolean。TrueString %>'){但在服务器端方法中没有返回bool值,因此上面的行永远不会等于true,因此deletelinkobj .close ("tr").hide('fast');//永远不调用隐藏行