2017-07-14 73 views
0

空購物車中,我使用Shopping in mvc from Microsoft website產品是在列表<cart>

這些都是我的模特和我的控制器

public class Cart 
{ 
    [Key] 
    public int Id { get; set; } 
    public string CartId { get; set; } 
    public int CProductId { get; set; } 
    public int Count { get; set; } 
    public DateTime DateCreated { get; set; } 
    public virtual ContainerPropertice CProducts { get; set; } 
} 

,這是myProduct的(containerPropertice)類。這是產品propertice具有與產品

關係,這是我在購物車類addtcart

public void AddToCart(ContainerPropertice cProduct) 
    { 
     // Get the matching cart and album instances 
     var cartItem = storeDB.Carts.SingleOrDefault(
      c => c.CartId == ShoppingCartId 
      && c.CProductId == cProduct.Id); 

     if (cartItem == null) 
     { 
      // Create a new cart item if no cart item exists 
      cartItem = new Cart 
      { 
       CProductId = cProduct.Id, 
       CartId = ShoppingCartId, 
       Count = 1, 
       DateCreated = DateTime.Now 

      }; 
      storeDB.Carts.Add(cartItem); 
     } 
     else 
     { 
      // If the item does exist in the cart, 
      // then add one to the quantity 
      cartItem.Count++; 
     } 
     // Save changes 
     storeDB.SaveChanges(); 
    } 
現在

當調試器到達的購物車/索引顯示籃下得到這個錯誤 是cProduct在購物車類在按鈕空 我把我的產品類和containerPropertice類

public class ContainerPropertice 
{ 
    public int Id { get; set; } 
    public int ProductId { get; set; } 
    public string Dimension { get; set; } 
    public decimal Weight { get; set; } 
    public decimal Price { get; set; } 
    public int Stoke { get; set; } 
    public string Description { get; set; } 
    public virtual Product Product { get; set; } 
    // public virtual ICollection<Cart> Carts { get; set; } 
} 

和產品模型類,圖片和名字從產品檔次得到,從containerpropertice的價格獲得class

public class Product 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 
    public string Summery { get; set; } 
    public string ProductDescription { get; set; } 
    public string Description { get; set; } 
    public virtual ProductCategory ProductCategory { get; set; } 
    public virtual IEnumerable<OrderDetail> OrderDetails { get; set; } 
    //public virtual IEnumerable<Cart> Carts { get; set; } 
    public virtual IEnumerable<ContainerPropertice> ContainerPropertices { get; set; } 

update1。我添加CSHTML的圖片

show error in view

更新2:這是指數shoppingcartcontroll

public virtual 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); 
    } 

這是ActionResult的是retirn我看來

public virtual ActionResult PDetails(int id) 
    { 
     var pp = _db.ContainerPropertices.Include(x=>x.Product).Where(x => x.ProductId == id); 
     int[] pCount = new int[pp.Count()]; 
     for (int i = 0; i < pCount.Length; i++) 
     { 
      pCount[i] = i + 1; 
     } 


     ViewBag.ProductCount = new SelectList(pCount); 
     ViewBag.ProductCounts = pCount.Length.ToString(); 
     int stokeIs = pp.Select(x => x.Stoke).FirstOrDefault(); 
     ViewBag.IsStoke = stokeIs; 
     if (stokeIs > 0) 
     { 
      ViewBag.IsStockStr = "In Stoke"; 
     } 
     if (stokeIs == 0) 
     { 
      ViewBag.IsStockStr = "Order"; 
     } 

     return View(pp.FirstOrDefault()); 
    } 
+0

我記得有人問過類似的問題,改變瀏覽器可以得到不同的結果,你可以試試嗎? –

+0

沒有。我改變它,但更好地描述我更新問題相同的問題 – sunny

回答

-2

你可能需要加載CProducts導航屬性在呈現視圖之前。 購物車控制器的Index()方法是怎樣的?