简体   繁体   中英

How to make action link created by razor make POST?

I use mvc 5 in my project.

I create action link using razor that sent some data to action method by GET mthod.

Here is action link:

<tbody>
    @foreach (var item in Model)
         {
               <tr><td>@Html.ActionLink(item.Name, "About", "Home", item, new { @class = "btn btn-featured btn-white height-30 width-100" })</td></tr>
         }
 </tbody>

My question is how to change created action link above to make it sent data by POST to action method?

You are using an HTML Anchor Link, which always do a GET action from Browser.

If your requirement is to do a post, you can use one of the below approaches:

1. Ajax Post

@Ajax.ActionLink("About", "Home", new { id = "myForm", @class = "btn btn-featured btn-white" }, new AjaxOptions { HttpMethod = "POST" })

For using @Ajax you will need to add a nuget reference from: https://www.nuget.org/packages/Microsoft.jQuery.Unobtrusive.Ajax/

This will add new scripts to your project (jquery.unobtrusive-ajax.min.js)

Without this, it will continue to use GET as your Method.

2. Form Submit Post

@using (Html.BeginForm("About", "Home", FormMethod.Post, new { id = "myForm", @class = "btn btn-featured btn-white" }))
{
   <a href="javascript:document.getElementById('myForm').submit()">
      <span>Submit</span>
   </a>
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM