javascript - MVC 4操作链接弹出框确认删除?

标签 javascript c# asp.net-mvc-4 popup jquery-ui-dialog

单击操作链接执行删除时执行确认弹出的最佳方式是什么?目前,当我按下删除操作链接时,它将直接删除。无论如何都要执行弹出确认框来删除? 谢谢!

我的观点:

  <td>
      @Html.ActionLink("Delete", "SURV_Question_Delete", "SURV_Question", new { Question_ID = Model[i].Question_ID }, null)
  </td>

我的 Controller :

 public ActionResult SURV_Question_Delete(int Question_ID)
        {

            var query = from r in db.SURV_Question_Ext_Model.ToList()
                        where r.Qext_Question_ID == Question_ID
                        select r;

            foreach(var item in query)
            { 
                db.SURV_Question_Ext_Model.Remove(item);
                db.SaveChanges();
            }

            return RedirectToAction("SURV_Main_Edit", "SURV_Main", new { id = surveyId });
        }

最佳答案

首先,您的删除操作应该是 POST,而不是 GET。您正在修改数据,并且不希望将其添加到浏览器历史记录中,或者允许在地址栏中输入网址(该项目可能已被删除)

将 html 更改为

@using (Html.BeginForm("SURV_Question_Delete", "SURV_Question", new { Question_ID = Model[i].Question_ID }))
{
  @Html.AntiForgeryToken()
  <input type="submit" value="delete" />
}

并使用 [HttpPost][ValidateAntiForgeryToken] 属性修饰该方法。

然后添加脚本

$('form').submit(function() {
  // Modify the following to use you jquery-ui-dialog, but for testing purposes
  if (!confirm("Are you sure want to delete record") { 
    return false; // cancel the submit if the user clicked the Cancel button
  }
});

关于javascript - MVC 4操作链接弹出框确认删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31645966/

相关文章:

sql-server - 连接到 SQL Server 时,数据源的值是什么?

javascript - 使用 Javascript 查找数组是否包含 5 个连续的数字

javascript - 如何在 jQuery 中对文本产生闪光效果?

javascript - 带 JQuery 的工具提示事件和带 id 的数组

javascript - 使用JS函数生成并渲染光标附近的html内容

c# - 使用C#动态缺少的编译器的ASP.NET Core 1.1编译所需的成员 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'

c# - 如何配置 EnyimMemcachedCore 以访问 AWS Lambda 中的 Elasticache?

c# - Service Fabric 应用程序中的静态变量范围

javascript - 将 MVC 模型转换为 Knockout JS ViewModel

asp.net - ASP.NET MVC 4 的 Windows 身份验证 - 它如何工作,如何测试它