使用 Ajax.ActionLink 执行正确的 HTTP 删除时出现问题

发布于 2024-11-27 16:24:03 字数 1202 浏览 2 评论 0原文

我正在尝试做什么: 尝试使用“正确的”HTTP 删除来删除记录。

控制器代码:

    [HttpDelete]
    public void DeleteRun(int RunId)
    {
        repository.RemoveEntry(RunId);

    }

Razor视图:

             @Ajax.ActionLink("Delete","DeleteRun",new {RunId = run.RunId},
                         new AjaxOptions() { Confirm = "Are you sure you want to delete this entry?",
                                            HttpMethod = "DELETE",
                                            OnComplete = string.Format("DeleteRunInTable({0})",run.RunId)

                         })

Javascript(在单独的包含文件中):

   function DeleteRunInTable(RunId) {
       $("tr[data-runid=" + RunId).remove();
}

正在创建的actionlink方法的链接:

 <a data-ajax="true" data-ajax-complete="DeleteRunInTable(11)" data-ajax-confirm="Are you sure you want to delete this entry?" data-ajax-method="DELETE" href="/Runs/Delete/11">Delete</a>

不确定javascript部分是否有效,但不用担心。尝试一次一步地迈出:)。现在它就像传统标签一样工作,当我单击链接时,它只是执行 href 的 GET 请求。当然,由于我在控制器上放置了 [HTTPDelete],我收到了 404 错误。我对网络开发还很陌生,所以我确信 javascript 或 jquery 还有其他方法可以做同样的事情,但我现在只是做我所知道的。

What i'm trying to do:
Try to delete a record using a "proper" HTTP Delete.

Controller Code:

    [HttpDelete]
    public void DeleteRun(int RunId)
    {
        repository.RemoveEntry(RunId);

    }

Razor View:

             @Ajax.ActionLink("Delete","DeleteRun",new {RunId = run.RunId},
                         new AjaxOptions() { Confirm = "Are you sure you want to delete this entry?",
                                            HttpMethod = "DELETE",
                                            OnComplete = string.Format("DeleteRunInTable({0})",run.RunId)

                         })

Javascript (in separate included file):

   function DeleteRunInTable(RunId) {
       $("tr[data-runid=" + RunId).remove();
}

Link the actionlink method is creating:

 <a data-ajax="true" data-ajax-complete="DeleteRunInTable(11)" data-ajax-confirm="Are you sure you want to delete this entry?" data-ajax-method="DELETE" href="/Runs/Delete/11">Delete</a>

Not sure if the javascript part works yet but not to worried about it. Trying to take it one step at a time :). Now its just working like a traditional tag and when i click the link its just doing a GET request of the href. Of course i get a 404 error because of the [HTTPDelete] i put on my controller. I'm pretty new to web development so i'm sure there are other ways in either javascript or jquery to do the same thing but i'm just doing what i know at this point.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦回梦里 2024-12-04 16:24:03

这应该可以工作,因为我最近自己做过,我所要做的就是在 AjaxOptions 参数中指定 HttpMethod

您还需要确保页面上包含 jquery.unobtrusive-ajax.js 脚本。

This should work as I have done it myself recently and all I had to do was specify the HttpMethod in the AjaxOptions argument.

You also need to ensure you have the jquery.unobtrusive-ajax.js script included on the page.

指尖上的星空 2024-12-04 16:24:03

这实际上是一个简单的解决方案......我缺少 jquery.unobtrusive-ajax.min.js :P。我将这篇文章留在这里,以便任何尝试做与我正在做的事情类似的事情的人都会知道它是可能的,只需确保您包含 jquery & jquery.不引人注目。

编辑:只是为了澄清如果您使用 MVC3,ActionLink 可以与 JQuery 一起使用,否则它使用 microsoft javascript 库。

It was actually a simple solution....i was missing the jquery.unobtrusive-ajax.min.js :P. I'm leaving the post here so anyone trying to do something similar to what i'm doing will know its possible just make sure you include jquery & jquery.unobtrusive.

Edit: Just to clarify ActionLink works with JQuery if your using MVC3 otherwise it uses the microsoft javascript libraries.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文