@Ajax.ActionLink is using GET instead of POST

馋奶兔 提交于 2019-12-24 03:37:17

问题


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>

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

Inside _Layout.cshtml I have

<body>

@RenderBody()

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

</body>

The bundle jquery val includes

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

回答1:


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.

Try using @Ajax.BeginForm(...){}.

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>

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") })

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



来源:https://stackoverflow.com/questions/22420512/ajax-actionlink-is-using-get-instead-of-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!