我正在尝试生成一个按钮,将项目添加到购物车,但用户将保持在同一页面,我尝试使用Ajax.ActionLink,但我仍然将用户重定向到购物车页面(我需要他留下来,只需添加项目) 这是Ajax
@Ajax.ActionLink("Add to cart",
"AddToCart",
"ShoppingCart",
new { id = item.TakeAwayId }, new AjaxOptions { HttpMethod = "POST", OnSuccess = "successFunction" }, "")
答案 0 :(得分:3)
只是另一种选择。使用JQuery ajax:
<a href="#" id="add-to-cart">Add to cart</a>
<script>
$(document).ready(function(){
$("#add-to-cart").click(function(e){
$.ajax({
url: "/ShoppingCart/AddToCart", //url path to the controller action
type: "POST",
data: {
id: "@item.TakeAwayId"
}
})
.success(function(data){
//do whatever you need to do with 'data'
});
e.preventDefault();
});
});
</script>
答案 1 :(得分:0)
我只是想对此进行猜测,并建议&#34; successFunction&#34;应该返回false