Ajax.ActionLink 未进行 POSTing
我正在尝试通过 POSTing 而不是 GETting 导航到 MVC 操作。 (该操作是删除,我不希望通过外部链接访问它。)
我正在使用由生成的网格中的链接
Ajax.ActionLink("Remove", "Delete", new { saID = Model.Said, id = e.id }, new AjaxOptions { HttpMethod = "POST", Confirm = "Are you sure you want to delete this item?" })
生成以下 HTML:
<a href="/Equipment/Delete/102424/229933" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, confirm: 'Are you sure you want to delete this item?', httpMethod: 'POST' });">Remove</a>
我的问题是,当我单击该链接时,我通过 GET 而不是 POST 到达删除操作,并且不会发生确认对话框。我已经在谷歌上搜索了几个小时,并且一直围绕着车轴。我做错了什么?
I am trying to navigate to an MVC action by POSTing rather than GETting. (The action is a DELETE, and I don't want it reachable by an external link.)
I am using a link in a grid generated by
Ajax.ActionLink("Remove", "Delete", new { saID = Model.Said, id = e.id }, new AjaxOptions { HttpMethod = "POST", Confirm = "Are you sure you want to delete this item?" })
Which generates the following HTML:
<a href="/Equipment/Delete/102424/229933" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.replace, confirm: 'Are you sure you want to delete this item?', httpMethod: 'POST' });">Remove</a>
My problem is that when I click on the link, I am reaching the Delete action via a GET rather than a POST, AND the Confirm dialog is not taking place. I have been googling this for several hours and just keep getting wrapped around the axle. What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的页面上可能存在其他一些 javascript 错误,导致处理 AJAX POST 的 javascript 无法运行。在这种情况下,链接将恢复为默认行为 (GET)。最简单的方法是使用 IE8 开发人员工具或 Firefox/Firebug(我更喜欢这些),并在页面加载或调用操作时查看控制台中是否有任何错误。如果您使用 IE,则需要使用
Internet 选项 ->高级
并取消选中禁用脚本调试
。修复你的 JavaScript 错误,我想它就会开始工作。
It's likely that you have some other javascript error on your page that is preventing the javascript that handles the AJAX POST from running. In this case, the link falls back to it's default behavior (GET). The easiest thing to do is use the IE8 developer tools or Firefox/Firebug (I prefer these) and look to see if you have any errors in the console when the page loads or your action is invoked. If you are using IE you'll need to use
Internet Options -> Advanced
and uncheckDisable script debugging
.Fix your javascript error and I think it will simply start working.
我想将其添加为已接受答案下的评论,但不知怎的,没有选项可以将其输入为评论,因此将其添加为答案
在我的情况下,我必须在 MicrosoftAjax 之前添加“jquery.unobtrusive-ajax.min.js”。 js 和 MicrosoftMvcAjax.js 然后操作链接开始发起 ajax 请求。但这是一个 GET,我想要发布,所以我只是在“AjaxOptions”中添加了
HttpMethod = "Post"
。就是这样。I wanted to add this as a comment under accepted answer but somehow there is no option to enter this as a comment so adding this as answer
In my case, I had to add "jquery.unobtrusive-ajax.min.js" before MicrosoftAjax.js and MicrosoftMvcAjax.js and then action link started initiating ajax request. But this was a GET and I wanted post so I just added
HttpMethod = "Post"
in the 'AjaxOptions`. That was it.