1

我对 MVC 和 Javascript 很陌生;我正在尝试进行删除操作;我正在使用带有 Javascript 函数的 ActionLink 进行确认。JAvascript 确认不起作用,即使我按下取消也会触发删除操作;此外,我似乎无法对动作使用 [HttpDelete] 动词:我需要将两个参数传递给 Delete 动作,但在动作链接上应用 @Html.HttpMethodOverride(Http.Delete) 后,它会搜索 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);
    }
4

3 回答 3

1

尝试这个

 @Html.ActionLink(
            "Delete", 
            "Delete", 
            new {id=notification.Id, typeId=notification.TypeId}, 
            new {onclick="return confirmDeleteAction();"})
于 2013-07-26T07:36:06.770 回答
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;
        }
于 2013-07-26T12:24:20.920 回答
0

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

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

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

true= 执行链接的默认行为

false= 什么都不做

于 2013-07-26T07:35:39.447 回答