拖放不放入购物车

发布于 2024-12-02 04:56:40 字数 1399 浏览 0 评论 0原文

我可以拖动专辑封面的图像,当它们从购物车按钮上掉落时,它们会克隆并返回到原来的位置,但是当我将其放在购物车按钮上时,它不会更新购物车,它只会返回到原来的位置状态。为什么会出现这种情况?

        $("#droppable").droppable({
            drop: function (event, ui) {
                var AlbumToAdd = ui.draggable.data("id");
                if (AlbumToAdd != '') {
                    // Perform the ajax post
                    $.post("/ShoppingCart/DragToCart", { "id": AlbumToAdd },
                        function (data) {
                            // Successful requests get here
                            // Update the page elements
                            $('#cart-status').text("Cart (" + data.CartCount + ")");
                        });
                }
            }
        });

控制器评论

//
// GET: /Store/DragToCart/5
public ActionResult DragToCart(int id)
{
    // Retrieve the album from the database
    var addedAlbum = storeDB.Albums
        .Single(album => album.AlbumId == id);

    // Add it to the shopping cart
    var cart = ShoppingCart.GetCart(this.HttpContext);

    cart.AddToCart(addedAlbum);

    var results = new DragToCartViewModel
    {
        Message = Server.HtmlEncode(addedAlbum.Title) +
            "Your cart has been updated",
        CartTotal = cart.GetTotal(),
        CartCount = cart.GetCount(),
        AddedId = id
    };
    return Json(results);

如果您想查看更多代码,请

I can drag the images of album art and they clone and go back to their original place when dropped away from the cart button, but when I drop it on the cart button it doesn't update the cart, it just goes back to its original state. Why is this occuring?

        $("#droppable").droppable({
            drop: function (event, ui) {
                var AlbumToAdd = ui.draggable.data("id");
                if (AlbumToAdd != '') {
                    // Perform the ajax post
                    $.post("/ShoppingCart/DragToCart", { "id": AlbumToAdd },
                        function (data) {
                            // Successful requests get here
                            // Update the page elements
                            $('#cart-status').text("Cart (" + data.CartCount + ")");
                        });
                }
            }
        });

Controller

//
// GET: /Store/DragToCart/5
public ActionResult DragToCart(int id)
{
    // Retrieve the album from the database
    var addedAlbum = storeDB.Albums
        .Single(album => album.AlbumId == id);

    // Add it to the shopping cart
    var cart = ShoppingCart.GetCart(this.HttpContext);

    cart.AddToCart(addedAlbum);

    var results = new DragToCartViewModel
    {
        Message = Server.HtmlEncode(addedAlbum.Title) +
            "Your cart has been updated",
        CartTotal = cart.GetTotal(),
        CartCount = cart.GetCount(),
        AddedId = id
    };
    return Json(results);

Comment if you want to see more code

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纵性 2024-12-09 04:56:40

您是否发现哪部分代码不起作用?是可放置的JS还是控制器?如果你把alert(“blah”);在你的 JS 中你可以找出哪些代码没有被触发。

Have you found out which part of the code doesn't work? Is it the droppable JS or the controller? If you put alert("blah"); in your JS you can find out which code doesn't get triggered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文