2010-06-14 87 views
13

我在控制器中声明的POST方法:MVC Ajax.ActionLink没有找到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限制,它工作正常。

我错过了什么?

回答

7

它似乎不喜欢我宣布我的OnFailureOnSuccess回调的方式。我想它无法解析我的AjaxOptions对象,因此忽略了HttpMethod="POST"设置。

我得到它的工作,改成:

OnFailure="function() { alert('fail'); }", 
OnSuccess="function() { alert('success'); }" 
0

的FormCollection有与之关联的默认粘结剂 总是 初始化集合,你应该 永远不会空。与使用表单 提交按钮相比,使用Ajax.ActionLink 时可能会有更多 收集空的 集合。这是因为当执行AJAX 请求时, ActionLink方法不会发布任何 表单值。 This post is the unswer to you question

4

我在这一刻学习ASP.MVC和我有这样的问题,我的Ajax.ActionLink,我得到了一个GET方法而不是POST方法,因为它应该已经。然后我意识到我没有添加脚本库参考:

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

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

+2

这些都是在MVC3和MVC4过时。请参阅:http://stackoverflow.com/a/8784998/210780 – ashes999 2013-04-18 17:44:08

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> 
+1

欢迎使用堆栈溢出!你能否详细说明为什么这会有所帮助?很好的答案很大一部分不仅仅是理解解决方案,而是理解为什么事情始终是一个问题! – tmesser 2012-10-26 14:57:07

+0

这些在MVC3和MVC4中已经过时。请参阅:http://stackoverflow.com/a/8784998/210780 – ashes999 2013-04-18 17:44:33