MVC Ajax.ActionLink 找不到 POST 方法

发布于 2024-09-05 18:10:22 字数 694 浏览 6 评论 0原文

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

我缺少什么?

I have a POST method declared in my controller:

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

and an ActionLink in my view:

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

I get a "not found" error when it tries to route this request.

If I remove the POST restriction from the UpdateComments method in the controller, it works fine.

What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染柒℉ 2024-09-12 18:10:22

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

我通过将其更改为:

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

It seems it didn't like the way I was declaring my OnFailure and OnSuccess callbacks. I guess it couldn't parse my AjaxOptions object so was ignoring the HttpMethod="POST" setting.

I got it working by changing it to:

OnFailure="function() { alert('fail'); }",
OnSuccess="function() { alert('success'); }" 
你又不是我 2024-09-12 18:10:22

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

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

添加脚本后它工作正常!

I am learning ASP.MVC at this moment and I had that issue with my Ajax.ActionLink, I got a GET method and not a POST method as it should had been. Then I realize that I didn't add the scripts library reference:

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

After I adding the script it worked fine!

薄荷梦 2024-09-12 18:10:22

尝试包括

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

try including

<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>
╰沐子 2024-09-12 18:10:22

FormCollection 有一个默认的活页夹
总是与之相关的
初始化集合,然后你
永远不应该为空。这是更多
可能你有一个空的
使用 Ajax.ActionLink 时的集合
与使用表单时相比
提交按钮。这是因为
ActionLink 方法不发布任何内容
执行 AJAX 时形成值
要求。
这篇文章是对您问题的解答

FormCollection has a default binder
associated with it which always
initializes the collection and you
should never get null. It is more
likely that you have an empty
collection when using Ajax.ActionLink
in contrast to when using a form
submit button. This is because the
ActionLink method doesn't POST any
form values when it performs the AJAX
request.
This post is the unswer to you question

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文