为什么ajax只是第一次执行

时间:2016-09-02 05:56:52

标签: javascript jquery ajax asp.net-mvc

有人可以解释为什么这只是第一次执行吗?它应该从minishopping购物车中移除物品,但它只是第一次工作。

这是html部分:

<div data-itemid="@item.Id" class="item @(i == 0 ? "first" : null) row"> 



                  <div class="remove">
                      <input class="remove-item-cart-btn" type="button" value="Remove" data-itemid="@item.Id" />
                  </div>

我的控制器中有动作:

[HttpPost]
    public ActionResult RemoveProductFromCart_Catalog(int id)
    {
        var shoppingCartItem = _workContext.CurrentCustomer.ShoppingCartItems
              .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart && sci.Id == id)
              .LimitPerStore(_storeContext.CurrentStore.Id).FirstOrDefault();
        _shoppingCartService.DeleteShoppingCartItem(shoppingCartItem, ensureOnlyActiveCheckoutAttributes: true);

        //display notification message and update appropriate blocks
        var updatetopcartsectionhtml = string.Format(_localizationService.GetResource("ShoppingCart.HeaderQuantity"),
             _workContext.CurrentCustomer.ShoppingCartItems
             .Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart)
             .LimitPerStore(_storeContext.CurrentStore.Id)
             .ToList()
             .GetTotalProducts());
        var updateflyoutcartsectionhtml = _shoppingCartSettings.MiniShoppingCartEnabled
            ? this.RenderPartialViewToString("FlyoutShoppingCart", PrepareMiniShoppingCartModel())
            : "";

        return Json(new
        {
            success = true,
            message = string.Format(_localizationService.GetResource("Products.ProductHasBeenAddedToTheCart.Link"), Url.RouteUrl("ShoppingCart")),
            updatetopcartsectionhtml = updatetopcartsectionhtml,
            updateflyoutcartsectionhtml = updateflyoutcartsectionhtml
        });
    }

这是js函数:

$(function() {
        $(".mini-shopping-cart").on("click", '.remove-item-cart-btn', function (e)
        {
            alert("test");
            var $this = $(this);

            $.ajax({                   
                url: '@Url.Action("RemoveProductFromCart_Catalog", "ShoppingCart")',
                cache: false,
                data:
                {
                    id: $this.data('itemid')
                },
                success: function (data, textStatus, jqXHR)
                {
                    $("#flyout-cart").replaceWith(data.updateflyoutcartsectionhtml);
                    jQuery.each($(".cart-qty"), function (i, val) {
                        $(val).text(data.updatetopcartsectionhtml);
                    });
                }
            });

            e.preventDefault();
        });
    });

我是js的新手,所以不要责怪我(很多)。

提前致谢!

这是整个html部分:

<div id="flyout-cart" class="flyout-cart">
<div class="mini-shopping-cart">
    <div class="count">
        @if (Model.TotalProducts == 0)
        {
            @T("ShoppingCart.Mini.NoItems")
        }
        else
        {
            @Html.Raw(string.Format(T("ShoppingCart.Mini.ItemsText").Text, string.Format("<a href=\"{0}\">{1}</a>", Url.RouteUrl("ShoppingCart"), string.Format(T("ShoppingCart.Mini.Items").Text, Model.TotalProducts))))
        }
    </div>
    @if (Model.TotalProducts > 0)
    {
        <div class="items">
            @for (int i = 0; i < Model.Items.Count; i++)
            {
                var item = Model.Items[i];
                <div data-itemid="@item.Id" class="item @(i == 0 ? "first" : null)">
                    @if (Model.ShowProductImages)
                    {
                        <div class="picture">
                            <a href="@Url.RouteUrl("Product", new { SeName = item.ProductSeName })" title="@item.Picture.Title">
                                <img alt="@item.Picture.AlternateText" src="@item.Picture.ImageUrl" title="@item.Picture.Title" />
                            </a>
                        </div>
                    }
                    <div class="product">
                        <div class="name">
                            <a href="@Url.RouteUrl("Product", new { SeName = item.ProductSeName })">@item.ProductName</a>
                        </div>
                        @if (!String.IsNullOrEmpty(item.AttributeInfo))
                        {
                            <div class="attributes">
                                @Html.Raw(item.AttributeInfo)
                            </div>
                        }
                        <div class="price">@T("ShoppingCart.Mini.UnitPrice"): <span>@item.UnitPrice</span></div>
                        <div class="quantity">@T("ShoppingCart.Mini.Quantity"): <span>@item.Quantity</span></div>
                    </div>
                    <div class="remove">
                        <input class="remove-item-cart-btn" type="button" value="Remove" data-itemid="@item.Id" />
                    </div>
                </div>
            }
        </div>
        <div class="totals">@T("ShoppingCart.Totals.SubTotal"): <strong>@Model.SubTotal</strong></div>
        <div class="buttons">
            @if (Model.DisplayShoppingCartButton)
            {
                <input type="button" value="@T("ShoppingCart.Mini.ViewCart")" class="button-1 cart-button" onclick="setLocation('@(Url.RouteUrl("ShoppingCart"))')" />

            }
            @if (Model.DisplayCheckoutButton)
            {
                var checkoutUrl = "";
                if (Model.AnonymousCheckoutAllowed && Model.CurrentCustomerIsGuest)
                {
                    checkoutUrl = Url.RouteUrl("LoginCheckoutAsGuest", new { returnUrl = Url.RouteUrl("ShoppingCart") });
                }
                else
                {
                    checkoutUrl = Url.RouteUrl("Checkout");
                }
                <input type="button" value="@T("Checkout.Button")" class="button-1 checkout-button" onclick="setLocation('@checkoutUrl')" />
            }
        </div>
    }
</div>

0 个答案:

没有答案
相关问题