存在的问题做了适当的HTTP删除与Ajax.ActionLink [英] Problems doing a proper HTTP Delete with Ajax.ActionLink

查看:129
本文介绍了存在的问题做了适当的HTTP删除与Ajax.ActionLink的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的: 尝试删除使用一个正确的HTTP删除的记录。

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

控制器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)

                         })

Java脚本(在单独的包含文件):

Javascript (in separate included file):

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

链接ActionLink的方法是创建:

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>

不知道的JavaScript的部分作品,不过也不要担心。试图把它一步一步的时间:)。现在,它的工作只是像一个传统的标签,当我点击链接它只是在做HREF的GET请求。当然,我得到,因为一个404错误[HTTPDelete]我把我的控制器。我是pretty的新的Web开发,所以我敢肯定还有其他的方式JavaScript或jQuery的做同样的事情,但我只是在做什么,我知道这一点。

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.

推荐答案

这应该是我最近做了我自己和我所要做的就是指定 HttpMethod AjaxOptions 的说法。

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

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

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

这篇关于存在的问题做了适当的HTTP删除与Ajax.ActionLink的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆