title: (Model.CartItems != null) Throwing Object reference not set to an instance of an object do we have any alternative to avoid this?
body: <p>I have an error in my partial view :  Object reference not set to an instance of an object which I tried to bybpass by adding this line but it seems not working because I got the previous mentionned error.  </p>
  
  <p>If I put a break point on the added line (@if (Model.CartItems != null)), the first time, the Model is not null (it contains the item I choose, I deleted it, the Model is NULL but the IF statement throw the error Object reference not set to an instance of an object.</p>
  
  <p>I go in my web store, I selected an item and I clicked remove this item.  The item is then deleted from the cart table.  But the view is not refreshing (the item is still on the screen.  But I received a message saying the item ABC was deleted.</p>
  
  <p>I added this line to try to avoid the object reference error for null but doesn't seems to be the proper command to use.  Any idea ?</p>
  
  <pre><code> @if (Model.CartItems != null)
  </code></pre>
  
  <p>TableContent.cshtml  partial view</p>
  
  <pre><code> @model Tp1WebStore3.ViewModels.ShoppingCartViewModel
  
   @{
      ViewBag.Title = "Table Content";
   }
  
   &lt;a Id="TableContent" href="#" class="TableContent"&gt;
     &lt;table&gt;
       &lt;tr&gt;
          &lt;th&gt;
              Produit
          &lt;/th&gt;
          &lt;th&gt;
              Prix (unitaire)
          &lt;/th&gt;
          &lt;th&gt;
              Quantite
          &lt;/th&gt;
          &lt;th&gt;&lt;/th&gt;
       &lt;/tr&gt;
       @if (Model.CartItems != null)   &lt;====  line which I added
       {
  
          foreach (var item in Model.CartItems)
          {
             &lt;tr id="row-@item.ProduitId"&gt;
                 &lt;td&gt;
                     @Html.ActionLink(item.Produit.Description, "Details", "Produit", new { id = 
                     item.ProduitId }, null)
                 &lt;/td&gt;
                 &lt;td&gt;
                     @item.Produit.Prix
                 &lt;/td&gt;
                 &lt;td id="item-count-@item.PanierId"&gt;
                     @item.Quantite
                 &lt;/td&gt;
                 &lt;td&gt;
                    &lt;a href="#" class="RemoveLink" data-id="@item.PanierId"&gt; Enlever du panier &lt;/a&gt;
                 &lt;/td&gt;
             &lt;/tr&gt;
           }
      }
  
         &lt;tr&gt;
            &lt;td&gt;
                 Total
            &lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td&gt;&lt;/td&gt;
            &lt;td id="cart-total"&gt;
                  @Model.CartTotal
            &lt;/td&gt;
         &lt;/tr&gt;
       &lt;/table&gt;
      &lt;/a&gt;
  </code></pre>
  
  <p>Index.cshtml   from Panier</p>
  
  <pre><code> @model Tp1WebStore3.ViewModels.ShoppingCartViewModel
  
   @{
       ViewBag.Title = "Shopping Cart";
   }
   &lt;script src="/Scripts/jquery-1.8.2.min.js" type="text/javascript"&gt;&lt;/script&gt;
  
   &lt;script type="text/javascript"&gt;
       $(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;
           });
       });
   &lt;/script&gt;
   &lt;h3&gt;
       &lt;em&gt;Details&lt;/em&gt; du panier:
   &lt;/h3&gt;
   &lt;p class="button"&gt;
       @Html.ActionLink("Checkout &gt;&gt;", "AddressAndPayment", "Checkout")
    &lt;/p&gt;  
    &lt;div id="update-message"&gt;
    &lt;/div&gt;
    &lt;div id="table-content"&gt;
        @Html.Partial("TableContent")    &lt;=== partial view call
    &lt;/div&gt;
  </code></pre>
  
  <p>PanierController.cs</p>
  
  <pre><code> 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 =&gt; 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 =&gt; 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");
           }
       }
   }
  </code></pre>