是否可以强制执行 @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/