2017-07-14 64 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()方法是怎样的?