如何创建加入购物车
How to create Add to Cart
我想做一个可以购买多件商品的简单在线商店。
这是我的代码
public void BuyItem(int deviceid, int quantity)
{
Dictionary<int, int> devicelist = new Dictionary<int, int>();
devicelist.Add(deviceid, quantity);
Device devices = (from device in master.Devices
where device.IDDevice == deviceid
select device).SingleOrDefault();
customer = os.GetCustomer(User);
//List<CartShop> cartList = new List<CartShop>();
//var toCart = devices.ToList();
//foreach (var dataCart in toCart)
//{
cartList.Add(new CartShop
{
IDDevice = deviceid,
IDLocation = devices.IDLocation,
IDCustomer = customer,
Name = devices.Name,
Quantity = quantity,
Price = Convert.ToInt32(devices.Price) * quantity
});
cartTotal = cartList;
StoreTransaksi.DataSource = new BindingList<CartShop>(cartTotal);
StoreTransaksi.DataBind();
//}
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.INFO,
Title = "INFO",
Message = "Success"
});
}
但它只能添加一项,选择另一项后,它会替换旧的。 (不能添加超过一个)。
请帮忙
问题在于 cartTotal 与 cartList 相同(查看 this)。您需要执行以下操作才能将列表复制到另一个列表而不保留引用:
cartTotal = new list<cartShop>(cartList);
另请注意,它仍在方法中,每次调用该方法时都会创建它。
更新:
这是一个非常简单的控制台应用程序,可以满足您的需求:
internal class Program
{
public static List<Item> ShoppingCart { get; set; }
public static void Main()
{
ShoppingCart = new List<Item>();
AddToCart(new Item() { ProductId = 2322, Quantity = 1 });
AddToCart(new Item() { ProductId = 5423, Quantity = 2 });
AddToCart(new Item() { ProductId = 1538, Quantity = 1 });
AddToCart(new Item() { ProductId = 8522, Quantity = 1 });
}
public static void AddToCart(Item item)
{
ShoppingCart.Add(new Item() { ProductId = item.ProductId, Quantity = item.Quantity});
}
}
public class Item
{
public int ProductId { get; set; }
public int Quantity { get; set; }
}
我想做一个可以购买多件商品的简单在线商店。 这是我的代码
public void BuyItem(int deviceid, int quantity)
{
Dictionary<int, int> devicelist = new Dictionary<int, int>();
devicelist.Add(deviceid, quantity);
Device devices = (from device in master.Devices
where device.IDDevice == deviceid
select device).SingleOrDefault();
customer = os.GetCustomer(User);
//List<CartShop> cartList = new List<CartShop>();
//var toCart = devices.ToList();
//foreach (var dataCart in toCart)
//{
cartList.Add(new CartShop
{
IDDevice = deviceid,
IDLocation = devices.IDLocation,
IDCustomer = customer,
Name = devices.Name,
Quantity = quantity,
Price = Convert.ToInt32(devices.Price) * quantity
});
cartTotal = cartList;
StoreTransaksi.DataSource = new BindingList<CartShop>(cartTotal);
StoreTransaksi.DataBind();
//}
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.INFO,
Title = "INFO",
Message = "Success"
});
}
但它只能添加一项,选择另一项后,它会替换旧的。 (不能添加超过一个)。 请帮忙
问题在于 cartTotal 与 cartList 相同(查看 this)。您需要执行以下操作才能将列表复制到另一个列表而不保留引用:
cartTotal = new list<cartShop>(cartList);
另请注意,它仍在方法中,每次调用该方法时都会创建它。
更新: 这是一个非常简单的控制台应用程序,可以满足您的需求:
internal class Program
{
public static List<Item> ShoppingCart { get; set; }
public static void Main()
{
ShoppingCart = new List<Item>();
AddToCart(new Item() { ProductId = 2322, Quantity = 1 });
AddToCart(new Item() { ProductId = 5423, Quantity = 2 });
AddToCart(new Item() { ProductId = 1538, Quantity = 1 });
AddToCart(new Item() { ProductId = 8522, Quantity = 1 });
}
public static void AddToCart(Item item)
{
ShoppingCart.Add(new Item() { ProductId = item.ProductId, Quantity = item.Quantity});
}
}
public class Item
{
public int ProductId { get; set; }
public int Quantity { get; set; }
}