@ Ajax.ActionLink使用GET而不是POST [英] @Ajax.ActionLink is using GET instead of POST

查看:116
本文介绍了@ Ajax.ActionLink使用GET而不是POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很激动,即使经过数小时的研究也无法弄清这个简单的问题:

I am really bummed out that I can't figure out this simple problem even after hours of research:

@Ajax.ActionLink("Test", "Test", new AjaxOptions { HttpMethod = "Post" })

<a data-ajax="true" data-ajax-method="Post" href="/Home/Test">Test</a>

这很简单,但是即使我指定了POST,它也会向/Home/Test发出GET请求.

It's as simple as it can get but it makes a GET request to /Home/Test even though I specified POST.

我在_Layout.cshtml里面

Inside _Layout.cshtml I have

<body>

@RenderBody()

@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)

</body>

捆绑jquery val包括

The bundle jquery val includes

jquery.validate.js
jquery.validate.unobtrusive.js

推荐答案

通常,当您要提交数据时,可以使用表单和提交按钮.我认为在操作链接上使用POST方法不是一个好主意.

Usually when you want to submit data you use a form and a submit button. In my opinion it's not a good idea to use POST method on an action link.

尝试使用@Ajax.BeginForm(...){}.

在执行此操作之前,请确保已在web.config中启用了<add key="UnobtrusiveJavaScriptEnabled" value="true" />.选中此选项后,打开Web应用程序并查看源代码,并确保其中包含以下文件:

And before you do this, make sure you have enabled <add key="UnobtrusiveJavaScriptEnabled" value="true" /> in your web.config. After you check this, open your web application and look at the source code an make sure you have the following files included:

<script src="∼/Scripts/jquery-1.10.2.js"></script>
<script src="∼/Scripts/jquery.unobtrusive-ajax.js"></script>

还要确保您的浏览器已启用Javascript.

Make sure also that your browser has Javascript enabled.

如果以上方法都不能解决您的问题,请尝试将网址添加到选项中:

If none of this solves your problem, try adding the url to the options:

@Ajax.ActionLink("Test", "Test", new AjaxOptions { HttpMethod = "Post", Url = Url.Action("Test") })

这是由于用户禁用了Javascript,或者您错过了对jquery.unobtrusive-ajax.js的引用而导致的情况.

This is a fallback for the cases in either the user has Javascript disabled, either you missed the reference to jquery.unobtrusive-ajax.js.

这篇关于@ Ajax.ActionLink使用GET而不是POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆