在"从表格中删除记录"上显示确认消息
本文关键字:quot 显示 消息 记录 确认 表格 删除 | 更新日期: 2023-09-27 18:00:58
当用户从网格中删除记录时,我想显示一条确认消息,这是我实现的,但我有错误消息
使用下面的代码,记录被删除,但:
- 仍在网格中的记录我必须刷新才能看到它消失
-
我收到消息错误!即使记录被删除3.
@Html.ActionLink("Delete Student", "Delete", new { @StudentID = StudentID }, new { @class="glyphicon glyphicon-pencil", @id=StudentID }) $(document).ready(function () { $('a.delete').click(OnDeleteClick); }); function OnDeleteClick(e) { var StudentId = e.target.id; var flag = confirm('You are about to delete this record permanently. Are you sure you want to delete this record?'); if (flag) { $.ajax({ url: '/Home/DeleteRecord', type: 'POST', data: { StudentID: StudentId }, dataType: 'json', success: function (result) { alert(result); $("#" + StudentId).parent().parent().remove(); }, error: function () { alert('Error!'); } }); } return false; }
控制器:
public ActionResult DeleteRecord(string StudentID)
{
//Code to delete
}
return RedirectToAction("StudentGrid",
"Home");
}
在看不到您使用的网格的情况下,尝试以下操作:
获取最接近的tr标签,这样您就可以在成功时将其删除:
var $tr = $(this).closest("tr");
$tr.remove();
jsFiddle
从控制器设置内容消息,重定向将不起作用,因为它是一个ajax调用。
public ActionResult DeleteRecord(string StudentID)
{
var success = false;
//Code to delete
// then set success variable
if (success)
{
return Content("Deleted");
}
else
{
return Content("Failed");
}
}
然后,从成功处理程序中检查消息并在需要时删除,客户端代码将如下所示:
function OnDeleteClick(e)
{
e.preventDefault();
var $tr = $(this).closest("tr");
var StudentId = e.target.id;
var flag = confirm('You are about to delete this record permanently. Are you sure you want to delete this record?');
if (flag) {
$.ajax({
url: '/Home/DeleteRecord',
type: 'POST',
data: { StudentID: StudentId },
dataType: 'json',
success: function (result) {
if (result == "Deleted")
$tr.remove();
},
error: function () {
alert('Error!');
}
});
}
return false;
}
public ActionResult DeleteRecord(string StudentID)
{
//Code to delete
}
return Json("Record Is Delete", JsonRequestBehavior.AllowGet);
}
使用来自控制器的is响应,您可以在alert((中显示此MSG在项目中有了更新网格,你可以使用下面的代码就足够了
$(e).closest("tr").remove();