ASP.NET MVC 3:如何强制ActionLink的做HttpPost而不是HTTPGET? [英] ASP.NET MVC 3 : How to force an ActionLink to do a HttpPost instead of an HttpGet?

查看:117
本文介绍了ASP.NET MVC 3:如何强制ActionLink的做HttpPost而不是HTTPGET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能迫使 @ Html.ActionLink()做的 POST 代替 GET ?如果是这样,怎么样?

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

推荐答案

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

}

这篇关于ASP.NET MVC 3:如何强制ActionLink的做HttpPost而不是HTTPGET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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