产品在列表<cart>中的购物车中为空

时间:2017-07-14 09:07:24

标签: c# asp.net-mvc shopping-cart

我使用Shopping in mvc from Microsoft website

这些是我的模特和我的控制器

 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)类。这是与产品有关的产品特性

这是我在shoppingCart类中的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类

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

这是重新审视我的观点的行动结果

 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());
    }

1 个答案:

答案 0 :(得分:-2)

您可能需要在渲染视图之前加载CProducts导航属性。 Cart控制器的Index()方法怎么样?