Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 登录用户的购物车会话.Net MVC_C#_Asp.net_.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 登录用户的购物车会话.Net MVC

C# 登录用户的购物车会话.Net MVC,c#,asp.net,.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,.net,Asp.net Mvc,Asp.net Mvc 4,当用户登录时,我被卡在购物车会话上。情况是这样的 用户登录并将产品添加到购物车,但注销时购物车为空。我们如何确保该特定用户的购物车会话即使在用户注销并将项目保留在购物车中后仍然保持 这是购物车控制器的操作 public ActionResult Buy(int id) { ProductModel productModel = new ProductModel(); if (Session["cart"] == null) { List<Item&g

当用户登录时,我被卡在购物车会话上。情况是这样的

用户登录并将产品添加到购物车,但注销时购物车为空。我们如何确保该特定用户的购物车会话即使在用户注销并将项目保留在购物车中后仍然保持

这是购物车控制器的操作

public ActionResult Buy(int id)
{
    ProductModel productModel = new ProductModel();
    if (Session["cart"] == null)
    {
        List<Item> cart = new List<Item>();
        cart.Add(new Item { Product = productModel.find(id), Quantity = 1 });
        Session["cart"] = cart;
    }
    else
    {
        List<Item> cart = (List<Item>)Session["cart"];
        int index = isExist(id);
        if (index != -1)
        {
            cart[index].Quantity++;
        }
        else
        {
            cart.Add(new Item { Product = productModel.find(id), Quantity = 1 });
        }
        Session["cart"] = cart;
    }
    return RedirectToAction("Index");
}

public ActionResult Remove(int id)
{
    List<Item> cart = (List<Item>)Session["cart"];
    int index = isExist(id);
    cart.RemoveAt(index);
    Session["cart"] = cart;
    return RedirectToAction("Index");
}

private int isExist(int id)
{
    List<Item> cart = (List<Item>)Session["cart"];
    for (int i = 0; i < cart.Count; i++)
        if (cart[i].Product.Id.Equals(id))
            return i;
    return -1;
}
这是项目模型

public class Item
{
    public Product Product
    {
        get;
        set;
    }

    public int Quantity
    {
        get;
        set;

    }
}
这是用户模型

public class User
{
    [Key]
    public int Id { get; set; }
    public string FullName { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string AccountType { get; set; }
}

请对此提供帮助

注销后购物车为空,因为会话已被删除。 如果要持久保存购物车项目,应将其存储在某个位置

可能的解决办法:

  • 在客户端上使用以存储添加项的id
  • 将它们存储在数据库中,并在登录时将其添加到购物车中

  • 需要一些关于2的详细信息。将它们存储在数据库中,并在登录时将它们添加到购物车中。有人能帮上忙吗?需要一些关于2的详细信息。将它们存储在数据库中,并在登录时将它们添加到购物车中。有人能帮忙吗?
    public class User
    {
        [Key]
        public int Id { get; set; }
        public string FullName { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string AccountType { get; set; }
    }