asp.net-mvc - ASP.NET MVC 3 : How to force an ActionLink to do a HttpPost instead of an HttpGet?

标签 asp.net-mvc asp.net-mvc-3

是否可以强制执行 @Html.ActionLink()做一个POST而不是 GET ?如果是这样,如何?

最佳答案

ActionLink辅助方法将呈现 anchor标签,点击它总是一个 GET要求。如果你想让它成为 POST要求。您应该使用一点 javacsript 覆盖默认行为

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

现在有些jQuery代码
<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>

确保您有一个 Action HttpPost的方法类型来处理这个请求
[HttpPost]
public ActionResult Delete(int id)
{
  // do something awesome here and return something      
}

关于asp.net-mvc - ASP.NET MVC 3 : How to force an ActionLink to do a HttpPost instead of an HttpGet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11488442/

相关文章:

MVC3 中的 ASP.NET AJAX 控件?

asp.net-mvc - ASP.NET 身份和声明

c# - ASP.NET MVC 数据库无法访问

c# - 确保只有计划任务可以调用 Controller 操作

不等于约束的ASP.NET路由

c# - ASP.NET MVC 3 Razor 递归函数

c# - 通过 C# 代码发出 http 请求?

c# - 嵌套复杂类型的显示名称

html - WebGrid 内容列突然不换行

jquery - 将动态 json 对象传递给 C# MVC Controller