MVC:Javascript确认删除操作不起作用

时间:2013-07-26 07:33:24

标签: javascript asp.net-mvc

我对MVC和Javascript很新;我正在尝试删除操作;我正在使用带有Javascript功能的ActionLink进行确认。 JAvascript确认不起作用,即使我按下取消也会触发删除操作;另外,我似乎无法使用[HttpDelete]动词来执行操作:我需要将两个参数传递给Delete操作,但在操作链接上应用@ Html.HttpMethodOverride(Http.Delete)后,它会搜索 对于只有一个parmae​​ter的URL:id。

这是我的代码:动作链接

 @Html.HttpMethodOverride(HttpVerbs.Delete)
  @Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="confirmDeleteAction()"})

function confirmDeleteAction() {       
    return confirm('Are you sure you want to delete the notification ?');        
}

删除操作:

   [HttpDelete]
    public ActionResult Delete(int id, int typeId)
    {
        Service.DeleteNotification(id, typeId);

        NewIndexViewModel model = Service.GetNewIndexViewModel();
        return View("Index", model);
    }

3 个答案:

答案 0 :(得分:1)

试试这个

 @Html.ActionLink(
            "Delete", 
            "Delete", 
            new {id=notification.Id, typeId=notification.TypeId}, 
            new {onclick="return confirmDeleteAction();"})

答案 1 :(得分:0)

当用户点击取消时,您需要取消浏览器的默认行为。您可以将confirmDeleteAction()的结果返回到<a>标记:

,从而完成此操作
@Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="return confirmDeleteAction()"})

为清楚起见,我将return关键字添加到onclick处理程序中,该处理程序会将confirmDeleteAction()的结果返回给浏览器 -

true =执行链接的默认行为

false =什么都不做

答案 2 :(得分:0)

你也可以这样做。

@Html.ActionLink(
                "Delete", 
                "Delete", 
                new {id=notification.Id, typeId=notification.TypeId}, 
                new {onclick="confirmDeleteAction(" + notification.Id + ")" })


function OnDeleteClick(id) {
                    var ActionID = id;
                    var flag = confirm('Are you sure you want to delete this record?');
                    if (flag) {
                return true;
                }
        else{
        return false;
        }