13

我在控制器中声明了一个 POST 方法:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UpdateComments(int id, string comments)
{
    // ... 
}

在我看来,还有一个 ActionLink:

<%= Ajax.ActionLink("update", "UpdateComments", 
                        new { id = Model.Id, comments = "test" }, 
                        new AjaxOptions { 
                                HttpMethod="POST", 
                                OnFailure="alert('fail');", 
                                OnSuccess = "alert('success');" 
                            })%>

当它尝试路由此请求时,我收到“未找到”错误。

如果我从控制器中的 UpdateComments 方法中删除 POST 限制,它工作正常。

我错过了什么?

4

4 回答 4

7

似乎它不喜欢我声明我的OnFailureOnSuccess回调的方式。我猜它无法解析我的AjaxOptions对象,所以忽略了HttpMethod="POST"设置。

我通过将其更改为:

OnFailure="function() { alert('fail'); }",
OnSuccess="function() { alert('success'); }" 
于 2010-06-14T11:59:28.383 回答
4

我现在正在学习 ASP.MVC,我的 Ajax.ActionLink 遇到了这个问题,我得到了一个 GET 方法,而不是应该的 POST 方法。然后我意识到我没有添加脚本库引用:

<script src=”/Scripts/MicrosoftAjax.js” type=”text/javascript”&gt;</script>
<script src=”/Scripts/MicrosoftMvcAjax.js” type=”text/javascript”&gt;</script>

添加脚本后,它工作正常!

于 2011-04-29T18:53:07.420 回答
0

尝试包括

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftAjax.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/MicrosoftMvcAjax.js")" type="text/javascript"></script>
于 2012-01-10T00:46:34.720 回答
0

FormCollection 有一个与之关联的默认绑定器,该绑定器始终初始化集合,并且您永远不应该为 null。与使用表单提交按钮时相比,使用 Ajax.ActionLink 时您更有可能拥有一个空集合。这是因为 ActionLink 方法在执行 AJAX 请求时不会 POST 任何表单值。 这篇文章是你问题的答案

于 2011-01-20T14:24:33.137 回答