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

将商品添加到购物车

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.

    评论

报告相同问题?