从网格中删除记录时显示确认消息
我想在用户从网格中删除记录时显示确认消息,这是我实现的但是我有错误消息
使用下面的代码删除记录但是:
- 仍然在网格中的记录我必须刷新才能看到它消失;
-
我有消息错误! 即使删除记录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); }
with是来自控制器的响应,您可以在alert()中显示此MSG,并在项目中使用更新网格,您可以使用下面的代码就足够了
$(e).closest("tr").remove();
- 使用MVC方法无法正确加载jPlayer歌曲
- 无法使用asp.net mvc url.action链接显示引导程序对话框
- 尽管asp.net mvc 4validation失败,jquerypost仍然经历过
- 0x800a138f – JavaScript运行时错误:属性’$’的值为null或undefined,而不是Function对象
- 突出显示母版页中的活动菜单项
- WebMethod返回JSON,但我的$ .ajax()回调中的响应obj只是一个字符串
- 在jquery.click()中设置ASP.NET会话
- 在Visual Studio 2013项目中实现jQuery插件
- jquery toggle / show hide