【发布时间】:2016-03-28 22:25:45
【问题描述】:
这是我添加到购物车的代码。现在我希望将会话对象保存在数据库中。有人可以向我解释我应该如何处理它
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);
}
【问题讨论】:
标签: c# asp.net-mvc-4 ef-code-first