[英]Can't GET my ajax “add to Cart Partial” function to work in ASP.NET MVC
I'm looking to call an ajax function to add to my cart partial I have made however it doesn't seem to be working. 我想调用ajax函数以将添加到我的购物车中的部分内容添加到购物车中,但它似乎无法正常工作。 I think the product Id isn't being linked to it for some reason. 我认为产品ID由于某种原因未与其链接。 Here is the code: 这是代码:
<div class="addtocart">
<a href="#" class="addtocart">Add to cart</a>
<span class="ajaxmsg">The product has been added to your cart. </span>
</div>
<script>
$(function () {
/*
* Add to cart
*/
$("a.addtocart").click(function (e) {
e.preventDefault();
$("span.loader").addClass("ib");
var url = "/cart/AddToCartPartial";
$.get(url, { id: @Model.Id }, function (data) {
$(".ajaxcart").html(data);
}).done(function () {
$("span.loader").removeClass("ib");
$("span.ajaxmsg").addClass("ib");
setTimeout(function () {
$("span.ajaxmsg").fadeOut("fast");
$("span.ajaxmsg").removeClass("ib");
}, 1000);
});
});
</script>
I have found a solution but When I use this link it works but it takes to an addtocartpartial which I don't want. 我找到了一个解决方案,但是当我使用此链接时,它可以工作,但需要添加到我不想要的addtocartpartial中。
@Html.ActionLink("Test", "AddtoCartPartial", "Cart", new { id = Model.Id }, new { @class = "addtocart" })
Is there another way of calling the ajax script or a way of avoiding the link to taking me to the addtocartpartial page on select? 还有另一种方法来调用ajax脚本,或者避免链接到select上的addtocartpartial页面的链接吗?
My controller for addtocartpartial is: 我的addtocartpartial控制器是:
public ActionResult AddToCartPartial(int id)
{
// Init CartVM list
List<CartVM> cart = Session["cart"] as List<CartVM> ?? new List<CartVM>();
// Init CartVM
CartVM model = new CartVM();
using (Db db = new Db())
{
// Get the product
ProductDTO product = db.Products.Find(id);
// Check if the product is already in cart
var productInCart = cart.FirstOrDefault(x => x.ProductId == id);
// If not, add new
if (productInCart == null)
{
cart.Add(new CartVM()
{
ProductId = product.Id,
ProductName = product.Name,
Quantity = 1,
Price = product.Price,
Image = product.ImageName
});
}
else
{
// If it is, increment
productInCart.Quantity++;
}
}
// Get total qty and price and add to model
int qty = 0;
decimal price = 0m;
foreach (var item in cart)
{
qty += item.Quantity;
price += item.Quantity * item.Price;
}
model.Quantity = qty;
model.Price = price;
// Save cart back to session
Session["cart"] = cart;
// Return partial view with model
return PartialView(model);
}
You may have a default route set for id
parameters. 您可能为id
参数设置了默认路由。 In that case you could append the value to the url in the format controller/action/{id}
, and remove the parameters from the $.get
. 在这种情况下,您可以将值附加到格式为controller/action/{id}
的url中,并从$.get
删除参数。 The code below may work for you: 以下代码可能适用于您:
var url = "/cart/AddToCartPartial/" + "@Model.Id";
$.get(url, function (data) {
$(".ajaxcart").html(data);
}).done(function () {
// ... other code
});
Or you could try appending the id
using query parameter style: 或者,您可以尝试使用查询参数样式附加id
:
var url = "/cart/AddToCartPartial?id=" + "@Model.Id";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.