My ajax is
<script>
jQuery(function ($) {
var size = $('#ddlsize').val();
var color = $('#ddlcolor').val();
$('#addtocart').click(function () {
$.ajax({
url: '@Url.Action("AddTocart", "ShoppingCart")',
data: {
id: '@Model.ProductId',
size: size,
color: color,
},
dataType: "html",
type: 'POST',
success: function (data) {
alert("Da them vao gio hang");
},
error: function () {
alert("Co loi xay ra vui long thu lai");
}
});
});
});
</script>
And in Controller
public ActionResult AddTocart(int id, string size, string color)
{
Product productitem = dbcon.Products.Where(p => p.ProductId == id).SingleOrDefault();
var cart = ShoppingCart.Getcart(this.HttpContext);
cart.AddtoCart(productitem, size, color);
return RedirectToAction("Index");
}
Some time when i click in button add to cart,i see the error but it has been do an action and add to the cart.i didn't how it add product to cart when error