weixin_33699914 2015-11-12 13:50 采纳率: 0%
浏览 344

将商品添加到购物车

I'm building a webshop in C# (MVC). Now I want to change the number of items that is shown in the cart-icon (at the layout-page). I've found this code and want to customize it to my page.

$(function () {
    // Document.ready -> link up remove event handler
    $("#@product.ArtNumber").click(function () {
        // Get the id from the link
        var itemToAdd = $(this).attr("data-id");
        if (itemToAdd != '') {
            // Perform the ajax post
            $.post("/ShoppingCart/RemoveFromCart", { "id": recordToDelete },
                function (data) {
                    // Successful requests get here
                    // Update the page elements
                    if (data.ItemCount == 0) {
                        $('#row-' + data.DeleteId).fadeOut('slow');
                    } else {
                        $('#item-count-' + data.DeleteId).text(data.ItemCount);
                    }
                    $('#cart-total').text(data.CartTotal);
                    $('#update-message').text(data.Message);
                    $('#cart-status').text('Cart (' + data.CartCount + ')');
                });
        }
    });
});

Now I have a question about " $.post("/ShoppingCart/RemoveFromCart" "

In my case I don't want to switch to another page when you click the button - but add an item to the cart (at the layout-page). What can I write instead of "post"? As I've understand this is used when you want to open a new HTML-page?

  • 写回答

1条回答 默认 最新

  • weixin_33675507 2015-11-12 14:01
    关注

    The $.post is shorthand of jQuery.post. http://api.jquery.com/jquery.post/

    It is an ajax call, and occurs in the background asynchronously. You are expected to have an endpoint that will handle the HTTP post on the server. Ideally this will update the server with the cart information such that it is available later if the client disconnects or closes their browser.

    In my case I don't want to switch to another page when you click the button

    I failed to actually answer your question directly. It will not take you to a different page or navigate away from the current page. You can actually open a new tab and paste that URL into the browser and see that it returns JSON.

    评论

报告相同问题?

悬赏问题

  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条