2014-03-13 22 views
0

我碰到從購物車中刪除後,出現此錯誤,對象引用未設置爲對象的實例。這在我的部分視圖中發生。任何想法如何解決這個問題?部分視圖錯誤對象引用未設置不明白爲什麼

TableContent.cshtml從帕尼埃

@model Tp1WebStore3.ViewModels.ShoppingCartViewModel 

@{ 
    ViewBag.Title = "Table Content"; 
} 

<a href="#" class="TableContent"> 
    <table> 
     <tr> 
      <th> 
       Produit 
      </th> 
      <th> 
       Prix (unitaire) 
      </th> 
      <th> 
       Quantite 
      </th> 
      <th></th> 
     </tr> 
     @foreach (var item in Model.CartItems) <=== error is happening here 
     { 
      <tr id="[email protected]"> 
       <td> 
        @Html.ActionLink(item.Produit.Description, "Details", "Produit", new { id = 
         item.ProduitId }, null) 
       </td> 
       <td> 
        @item.Produit.Prix 
       </td> 
       <td id="[email protected]"> 
        @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> 
</a> 

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).remove(); 
        $('#row-' + result.DeleteId).fadeOut('slow'); 
        $('#cart-status').text('Cart (' + result.CartCount + ')'); 
        $('#update-message').text(result.Message); 
        $('#cart-total').text(result.CartTotal); 
        $.get('@Url.Action("TableContent", "Panier")') 
          $("#TableContent").html(data); }); 
        }, 
        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> 
    <div id="table-content"> 
     @Html.Partial("TableContent") <=== partial view call 
    </div> 

PanierController.cs(部分圖)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using Tp1WebStore3.Models; 
using Tp1WebStore3.ViewModels; 

namespace Tp1WebStore3.Controllers 
{ 
    public class PanierController : Controller 
    { 
     // 
     // GET: /Panier/ 
     Tp1WebStoreDBEntities dbProduit = new Tp1WebStoreDBEntities(); 

     // 
     // GET: /ShoppingCart/ 
     public ActionResult Index() 
     { 
      var cart = ShoppingCart.GetCart(this.HttpContext); 

      // Set up our ViewModel 
      var viewModel = new ShoppingCartViewModel 
      { 
       CartItems = cart.GetCartItems(), 
       CartTotal = cart.GetTotal() 
      }; 
      // Return the view 
      return View(viewModel); 
     } 
     // 
     // GET: /Store/AddToCart/5 
     public ActionResult AddToCart(int id) 
     { 
      // Retrieve the album from the database 
      var addedProduit = dbProduit.Produits 
        .Single(produit => produit.ProduitId == id); 

      // Add it to the shopping cart 
      var cart = ShoppingCart.GetCart(this.HttpContext); 

      cart.AddToCart(addedProduit); 

      // Go back to the main store page for more shopping 
      return RedirectToAction("Index"); 
     } 
     // 
     // 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); 
     /* return View("CartSummary"); */ 
     } 
     // 
     // GET: /ShoppingCart/CartSummary 
     [ChildActionOnly] 
     public ActionResult CartSummary() 
     { 
      var cart = ShoppingCart.GetCart(this.HttpContext); 

      ViewData["CartCount"] = cart.GetCount(); 
      return PartialView("CartSummary"); 
     } 

     public ActionResult TableContent() 
     { 
       return PartialView("TableContent"); 
     } 
    } 
} 
+0

您可能需要檢查'cart.GetCartItems()'是否爲空。相關:http://stackoverflow.com/questions/3088147/why-does-net-foreach-loop-throw-nullrefexception-when-collection-is-null –

+0

@StevenV我刪除了panier項目,所以它現在是空的或null ?我只是想刷新視圖,因爲它保留了我應該刪除的選定項目。它已從數據庫中刪除,但屏幕不刷新它。我在屏幕上收到了消息「您的項目....已被刪除。」我希望視圖不再顯示已刪除的項目。 – user3127986

回答

0

你確定Model.CartItems被不是null?

試試這個:

@if (Model.CartItems != null) { <=== add breakpoint here 
    foreach (var item in Model.CartItems) <=== error is happening here 
    { 
     ... 
    } 
} 

,並在如果添加一個斷點,並檢查值爲null ;-)

編輯:在阿賈克斯性反應 ,可以重新裝載partialview .. 。

success: function (result) { 
$('#yourPartialViewContainerId').load('/Action/PartialView'); 
... 

但應確保你的foreach的項目是不是你的partialView內空...

+0

該項目已被刪除,購物車應該是空的或空的不確定。我想要局部視圖來刷新屏幕(只需放置標題並從屏幕上的視圖中刪除該項目,現在數據庫表購物車已被刪除,但屏幕沒有被刷新,我收到消息「您的項目。 ...已被刪除。「 – user3127986

+0

我編輯了我的答案 – Benoit

+0

如果CartItems爲空,我們在做什麼,因爲我只有一個項目,我將其刪除?這是我正在尋找的。謝謝 – user3127986

相關問題