从购物车中删除的Javascript无法正常刷新

时间:2014-03-10 14:15:05

标签: c# asp.net-mvc asp.net-mvc-4

当我从购物车中删除时,视图无法正常刷新。该项目已被删除,但我的屏幕仍显示产品名称,价格和数量。我期待这条生产线将彻底改造。我正在从platine.com尝试MVC音乐商店,但删除它似乎也无法正常工作(http://asp-net-mvc.platine.com/ShoppingCart#

有人可以解释一下为什么这不正确吗? 我找不到我在这里失踪的部分。 感谢

以下是我的输出结果:

 Details of Cart:

 Checkout >>
 -----------

 Baseball glove has been removed from your shopping cart.

 Product         Price(unit) Quantity
 Baseball glove  44.99       1         Remove from Cart   <== this line should be remove ?
 --------------                        ----------------   <== this line should be remove ?       
 Total                                 0                  <== this line should be remove

Index.cshtml

 @model Tp1WebStore3.ViewModels.ShoppingCartViewModel

 @{
   ViewBag.Title = "Shopping Cart";
 }
 <script src="/Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>

 <script type="text/javascript">
     $(function () {
         $('.RemoveLink').click(function () {
             $.ajax({
                 url: '/Panier/RemoveFromCart',
                 data: { id: $(this).data('id') },
                 type: 'POST',
                 cache: false,
                 success: function (result) {
                    $('#row-' + result.DeleteId).fadeOut('slow');
                    $('#cart-status').text('Cart (' + result.CartCount + ')');
                    $('#update-message').text(result.Message);
                    $('#cart-total').text(result.CartTotal);
                 },
                 error: function(XMLHttpRequest, textStatus, errorThrown) { 
                 alert("Status: " + textStatus); alert("Error: " + errorThrown); 
             });
             return false;
         });
     });
 </script>
 <h3>
    <em>Details</em> du panier:
 </h3>
 <p class="button">
     @Html.ActionLink("Checkout >>", "AddressAndPayment", "Checkout")
 </p>  
 <div id="update-message">
 </div>
 <table>
    <tr>
        <th>
            Produit
        </th>
        <th>
            Prix (unitaire)
        </th>
        <th>
            Quantite
        </th>
        <th></th>
    </tr>
    @foreach (var item in Model.CartItems)
    {
       <tr id="row-@item.ProduitId">
           <td>
              @Html.ActionLink(item.Produit.Description,"Details", "Produit", new { id = 
                      item.ProduitId }, null)
           </td>
           <td>
               @item.Produit.Prix
           </td>
           <td id="item-count-@item.PanierId">
               @item.Quantite
           </td>
           <td>
             <a href="#" class="RemoveLink" data-id="@item.PanierId"> Enlever du panier </a>    
           </td>
       </tr>
    }
    <tr>
      <td>
          Total
      </td>
      <td></td>
      <td></td>
      <td id="cart-total">
          @Model.CartTotal
      </td>
    </tr>
 </table>

PanierController.cs

    // AJAX: /ShoppingCart/RemoveFromCart/5
    [HttpPost] 
    public ActionResult RemoveFromCart(int id)
    {
        // Remove the item from the cart
        var cart = ShoppingCart.GetCart(this.HttpContext);

        // Get the name of the album to display confirmation
        string produitDescription = dbProduit.Paniers
            .Single(item => item.PanierId == id).Produit.Description;

        // Remove from cart
        int itemCount = cart.RemoveFromCart(id);

        // Display the confirmation message
        var results = new ShoppingCartRemoveViewModel
        {
            Message = Server.HtmlEncode(produitDescription) +
                " has been removed from your shopping cart.",
            CartTotal = cart.GetTotal(),
            CartCount = cart.GetCount(),
            ItemCount = itemCount,
            DeleteId = id
        };
        return Json(results);  

    }

ShoppingCartRemoveViewModel.cs

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;

 namespace Tp1WebStore3.ViewModels
 {
    public class ShoppingCartRemoveViewModel
    {
        public string Message { get; set; }
        public decimal CartTotal { get; set; }
        public int CartCount { get; set; }
        public int ItemCount { get; set; }
        public int DeleteId { get; set; }
    }
 }

以下是Google PF12 Network RemoveFromCart。这意味着我认为Javascript成功运行。 预览

 {Message:Baseball glove has been removed from your shopping cart., CartTotal:0, CartCount:0,..}
 CartCount: 0
 CartTotal: 0
 DeleteId: 131
 ItemCount: 0
 Message: "Baseball glove has been removed from your shopping cart."

响应

  {"Message":"Baseball glove has been removed from your shopping 
         cart.","CartTotal":0,"CartCount":0,"ItemCount":0,"DeleteId":131}

0 个答案:

没有答案