Error message in Part 8

May 13, 2013 at 2:10 PM
Edited May 13, 2013 at 2:13 PM
I'm trying to get through part 8 of the MvcMusicStore.

I've done all the required coding but when I click the 'Add To Cart' button, I get an error message:

NullReferenceException was unhandled by user code
Object reference not set to an instance of an object.

In the debugger it's saying the error is in index.cshtml (views\shoppingCart) line 53, col 6 ch 6.

Seems to be having issues with the "@foreach (var item in Model.CartItems)

Wondering if anyone can shed anylight on the situation.. have had a look at the completed code and it's exactly the same as what's in there.

Jan 18, 2015 at 6:40 PM
I am having exactly the same issue. It seems to be that the Cart object in CartItems does not have an Album object value.
The CartItem exists but EF does not seem to be populating the Album

From AddToCart method in ShppingCart model:
  cartItem = new Cart
                    AlbumId = album.StoreAlbumId,
                    CartId = ShoppingCartId,
                    Count = 1,
                    DateCreated = DateTime.Now
Cannot see where the EF will load this in later :
public List<Cart> GetCartItems()
return storeDB.StoreCarts.Where(cart => cart.CartId == ShoppingCartId).ToList();
If anyone has an answer please do enlighten me