在数据库中保存购物车的会话值因此,这是我添加到购物车的代码。现在我希望将会话对象保存在数据库中。有人可以向我解释我应该如何处理它如何使用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