在數據庫中保存購物車的會話值因此,這是我添加到購物車的代碼。現在我希望將會話對象保存在數據庫中。有人可以向我解釋我應該如何處理它如何使用asp.net + MVC
public ActionResult Cart(int id)
{
if (Session["cart"] == null)
{
var cart = new List<Item>();
cart.Add(new Item(_productService.GetProductById(id), 1));
Session["cart"] = cart;
}
else
{
var cart = (List<Item>)Session["cart"];
int index = isExisting(id);
if (index == -1)
{
cart.Add(new Item(_productService.GetProductById(id), 1));
}
else
{
cart[index].Quantity++;
}
Session["cart"] = cart;
}
var userModel = new UserViewModel(true, null, null);
return View(userModel);
}
非常感謝您的洞察力,就像在購物車會話中正在保存的這行代碼一樣「var cart =(List- )Session [」cart「];」。但是,我可以使用這個「var cart」實際上將它保存到數據庫中。 –
Jay