另一个空问题?在购物车中删除项目时 [英] Another null question? On delete item in cart

查看:65
本文介绍了另一个空问题?在购物车中删除项目时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码可以解决我遇到的问题,当购物车表中的购物车计数到1时.不会删除.它崩溃了.

您能协助解决代码或给我看一个超链接示例吗?

The code works the problem I am have is when the cart gets to 1 count in cart table. Will not delete. It crashes.

Can you assist with fixing code or show me with a hyperlink samples.

public int RemoveFromCart(int id)
        {
             DB.Cart cartItem =
                 _db.Carts.SingleOrDefault(
                      c => c.CartRecordId == ShoppingCartId && c.ProductId == id);

             int itemCount = 0;

             if (cartItem != null)
             {
                  if(cartItem.Count > 1)
                  {
                       cartItem.Count--;
                       itemCount = cartItem.Count;
                  }
                  else
                  {
                       _db.Carts.DeleteObject(entity: new DB.Cart
                                              {
                                                   ProductId = id,
                                                   CartRecordId = ShoppingCartId
                                                   //Count =-1
                                              });
                  }


                  _db.SaveChanges();
             }
             return itemCount;
        }



提前谢谢.



Thanks ahead of time.

推荐答案

DB.Cart cart = (from c in _db.Carts
                 where c.CartRecordId == ShoppingCartId && c.ProductId == id
                  select c).ToList()[0];
                  _db.DeleteObject(cart);


这篇关于另一个空问题?在购物车中删除项目时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆