[英]ASP.NET Remove all items from ShopCart
嘿,我有ShoppCart类,其中包含class cartItems类的项目
所以我可以运行像
ShoppingCart.Instance.AddItem(5, 5, 16.0, "First Product", 15, 10)
ShoppingCart.Instance.Items.Count
ShoppingCart.Instance.RemoveItem(productId)
删除功能的外观
' RemoveItem() - Removes an item from the shopping cart
Public Sub RemoveItem(ByVal productId As Integer)
Dim removedItem = New CartItem(productId)
For Each item As CartItem In Items
If item.Equals(removedItem) Then
Items.Remove(item)
Return
End If
Next
End Sub
那将删除一个项目,我正在查看一个可用于清空购物车的功能,即在注销时尝试了Session.Abandon,但随后再次登录它仍然记得购物车中的项目。
尚不清楚您的购物车中正在使用哪种列表,但您可以尝试-
Items.RemoveAll()
编辑
我认为'RemoveAll()'方法需要谓词作为参数,这意味着您需要执行类似的操作(虽然可以执行,但很奇怪)-
Items.RemoveAll(x => 1==1)
或在vb中
list.RemoveAll(Function(x) 1 = 1)
你可以试试这个-
Items.RemoveRange(0, Items.Count)
你可以简单地做到这一点
ShoppingCart.Instance.Items.RemoveAll()
编辑
不确定正在使用哪个集合。 我想另一种简单的方法是重新初始化集合
ShoppingCart.Instance.Items = new Items();
尝试调用ShoppingCart.Instance.Items.Clear();
只是一个随机的猜测,如果您使用的是此处提供的示例购物车代码: http : //www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=423 (因为它看起来很相似),那么您还可以执行以下操作: ShoppingCart类添加了一个新方法,该方法可以清除所有项目,如下所示:
public void RemoveAll() {
Items.Clear();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.