【问题标题】:MVC Ajax.ActionLink doesn't find POST methodMVC Ajax.ActionLink 找不到 POST 方法
【发布时间】:2011-03-03 11:44:34
【问题描述】:

我的控制器中声明了一个 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 限制,它可以正常工作。

我错过了什么?

【问题讨论】:

    标签: c# asp.net-ajax asp.net-mvc-2 routing actionlink


    【解决方案1】:

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

    我通过将其更改为:

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

    【讨论】:

      【解决方案2】:

      FormCollection 有一个默认的活页夹 与之相关的总是 初始化集合和你 永远不应该为空。这是更多 可能你有一个空的 使用 Ajax.ActionLink 时的集合 与使用表格时相比 提交按钮。这是因为 ActionLink 方法不发布任何内容 执行 AJAX 时的表单值 要求。 This post is the unswer to you question

      【讨论】:

        【解决方案3】:

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

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

        添加脚本后,它运行良好!

        【讨论】:

        【解决方案4】:

        尝试包括

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

        【讨论】:

        • 欢迎来到 Stack Overflow!您能否详细说明为什么这会有所帮助?一个好的答案的很大一部分不仅仅是理解解决方案,而是理解为什么事情是一个问题开始!
        • 这些在 MVC3 和 MVC4 中已过时。见:stackoverflow.com/a/8784998/210780
        猜你喜欢
        • 1970-01-01
        • 2012-09-13
        • 1970-01-01
        • 2011-10-07
        • 2014-01-26
        • 2014-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多