ASP.NET MVC 3: How to get ActionLink to do HttpPost instead of HttpGet?

Is it possible to force @Html.ActionLink() to do POST instead of GET ? If so, how?

+6
source share
5 answers

ActionLink helper method displays the anchor tag, clicking on which will always get a GET request. If you want to make a POST request. You should override the default behavior using a little javacsript

 @ActionLink("Delete","Delete","Item",new {@id=4},new { @class="postLink"}) 

Now some jQuery code

 <script type="text/javascript"> $(function(){ $("a.postLink").click(function(e){ e.preventDefault(); $.post($(this).attr("href"),function(data){ // got the result in data variable. do whatever you want now //may be reload the page }); }); }); </script> 

Make sure you have an HttpPost Action method to handle this request.

 [HttpPost] public ActionResult Delete(int id) { // do something awesome here and return something } 
+9
source

I suggest that if you need something like this for an Action that will do something โ€œpersistentโ€ on the server side. For example, deleting an object in a database.

Here is a complete removal example using the link and publication: http://www.squarewidget.com/Delete-Like-a-Rock-Star-with-MVC3-Ajax-and-jQuery

From the previous link (recommended reading anyway):

Delete link in your opinion:

 @Ajax.ActionLink("Delete", "Delete", "Widget", new {id = item.Id}, new AjaxOptions { HttpMethod = "POST", Confirm = "Are you sure you want to delete this widget?", OnSuccess = "deleteConfirmation" }) 

A bit of JS:

 function deleteConfirmation(response, status, data) { // remove the row from the table var rowId = "#widget-id-" + response.id; $('.widgets').find(rowId).remove(); // display a status message with highlight $('#actionMessage').text(response.message); $('#actionMessage').effect("highlight", {}, 3000); } 
+7
source

What I would do is wrap the html around the form

 @using(Html.BeginForm("YourAction","YourController", FormMethod.Post)){ <button>Hello</button> } 

Instead of using a link, you can use a button.

If you really want to use the link, you may need javascript

Something like that:

 $("#idOfYourLink").click(function(){ var form = $(this).parents('form:first'); form.submit(); }); 
+4
source

It is not possible for the <a> element to perform POST on the web server.

You can use Javascript to capture the click event, stop navigation and perform AJAX POST on the server, but if the user has Javascript disabled, nothing will happen.

Do you need to use the <a> element or just something similar to the <a> element?

Also worth mentioning is AjaxLink . This allows you to easily use the <a> element to perform an AJAX POST.

+1
source

If you think ... there is no tag for a link in HTML that does POST. And why can't you make the link do POST (and that makes no sense). To use "POST", ou must "POST" something. And something should be a form, or you can do a POST using the javascript function for AJAX. In any case, if you need to READ without publishing anything, you should look at your resource model, something stinks.

0
source

Source: https://habr.com/ru/post/920510/


All Articles