本文整理汇总了C#中CartItem类的典型用法代码示例。如果您正苦于以下问题:C#CartItem类的具体用法?C#CartItem怎么用?C#CartItem使用的例子?这里精选的类代码示例或许可以为您提供帮助。
示例1:AddToCart
publicvoidAddToCart(intid)
{
//Retrievetheproductfromthedatabase.
ShoppingCartId=GetCartId();
varcartItem=_db.ShoppingCartItems.SingleOrDefault(
c=>c.CartId==ShoppingCartId
&&c.ProductId==id);
if(cartItem==null)
{
//Createanewcartitemifnocartitemexists.
cartItem=newCartItem
{
ItemId=Guid.NewGuid().ToString(),
ProductId=id,
CartId=ShoppingCartId,
Product=_db.Products.SingleOrDefault(
p=>p.ProductID==id),
Quantity=1,
DateCreated=DateTime.Now
};
_db.ShoppingCartItems.Add(cartItem);
}
else
{
//Iftheitemdoesexistinthecart,
//thenaddonetothequantity.
cartItem.Quantity++;
}
_db.SaveChanges();
}
原文链接:
http://www.jxszl.com/biancheng/C/556680.html