从iframe调用delete方法时获取404

时间:2015-04-12 14:24:23

标签: html asp.net-mvc iframe razor

我正试图在用户点击

上调用删除方法
 @Html.ActionLink(
                    " ",
                       "DeleteAttachment",
                    new { attachmentid = item.bookingattachmentid },
                    new {
                        @class = "buttondelete" 
                })

当我将鼠标悬停在删除链接上时,会显示正确的网址...

 http://localhost:53712/Booking/DeleteAttachment?attachmentid=51693

但点击后,服务器会回复“找不到资源。”

在搜索Google时,我没有发现任何说法我无法从iframe调用删除帖子。 iframe的源是与父级相同的域。

这是控制器动作

 [HttpPost]
    public string DeleteAttachment(int bookingattachmentid)
    {
        bookingattachment bookingattachment = db.bookingattachments.Find(bookingattachmentid);
        db.bookingattachments.Remove(bookingattachment);
        db.SaveChanges();
        return "success";
    }

1 个答案:

答案 0 :(得分:1)

您的控制器操作似乎是用[HttpPost]动词修饰的。因此,为了调用它,您需要使用POST HTTP动词。在ASP.NET MVC中,Html.ActionLink生成一个锚,后者又使用GET动词。这就是你获得404的原因。

如果您希望能够调用此控制器操作,则可以使用HTML表单:

@using (Html.BeginForm("DeleteAttachment", "Booking", new { attachmentid = item.bookingattachmentid }, FormMethod.Post))
{
    <input type="submit" class="buttondelete" value="Delete Attachment" />
}

如果您不想使用HTML <form>,可以考虑使用AJAX来调用控制器操作,因为它允许您使用POST动词:

@Ajax.ActionLink(
    linkText: " ", 
    actionName: "DeleteAttachment", 
    controllerName: "Booking", 
    routeValues: new { attachmentid = item.bookingattachmentid }, 
    ajaxOptions: new AjaxOptions { HttpMethod = "POST" }
)
相关问题