且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ASP.NET MVC 3:如何强制ActionLink的做HttpPost而不是HTTPGET?

更新时间:2022-11-27 22:09:13

ActionLink的 helper方法将呈现一个标签,点击这始终是一个 GET 请求。如果你想成为一个 POST 请求。你应该用一个小javacsript覆盖缺省behviour

ActionLink helper method will render an anchor tag, clicking on which is always a GET request. If you want to make it a POST request. You should override the default behviour using a little javacsript

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

现在一些的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>

请确保您有 HttpPost 动作方法类型来处理这个请求

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

[HttpPost]
public ActionResult Delete(int id)
{
  //do soome thing awesome here and return something

}