从卡中删除 Ajax 方法在音乐商店购物卡的 MVC4 教程中不起作用

本文关键字:教程 不起作用 MVC4 音乐 Ajax 删除 方法 | 更新日期: 2023-09-27 18:37:16

我按照MVC4音乐商店教程制作购物卡。不幸的是,当我使用RemoveFromCard功能时,我收到以下消息:

The resource cannot be found. 
  Description: HTTP 404. The resource you are looking for (or one of its  
  dependencies) could have been removed, had its name changed, or is temporarily   
  unavailable. Please review the following URL and make sure that it is spelled 
  correctly. 
Requested URL: /ShoppingCart/RemoveFromCart/1

我在视图页面上使用的代码如下:

<script src="/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        // Document.ready -> link up remove event handler
        $(".RemoveLink").click(function () {
            // Get the id from the link
            var recordToDelete = $(this).attr("data-id");
            if (recordToDelete != '') {
                // 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 + ')');
                });
            }
        });
    });
</script>

使用以下操作链接:

<td>
                <%: Ajax.ActionLink("Remove from cart", "RemoveFromCart",
                        new { id = item.RecordId },
                        new AjaxOptions { OnSuccess = "handleUpdate" })%>
            </td>

此外

我在购物卡中有这种方法.cs

public int RemoveFromCart(int id) //code

为什么找不到这种方法?我将不胜感激!

从卡中删除 Ajax 方法在音乐商店购物卡的 MVC4 教程中不起作用

好的,

我解决了问题...我将操作链接更改为:

<a href="#" class="RemoveLink" data-id="<%:item.RecordId %>">Remove from cart</a>

现在它正在工作。