[英]How to save session values of shopping cart in database using 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);
}
有幾種方法可以通過 ASP.NET 將數據保存到數據庫。
我建議您考慮使用對象關系映射工具:
每個工具都有大量的文檔資源。
您也可以使用 ADO.NET,但不建議這樣做,除非您絕對必須使用它。
我處理這個確切的場景。 一種選擇是將您的購物車序列化為 XML 並將 XML 存儲在數據庫列中。 如果您不希望需要查詢對象的詳細信息,則此方法很有用 - 您只想保存和恢復它,這就是您對 Session 所做的。 缺點是如果您修改構成購物車的一個或多個類,您可能會失去一些向后兼容性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.