【问题标题】:session passing to Class File and retrieve by gridview会话传递给类文件并由 gridview 检索
【发布时间】:2013-11-24 03:41:41
【问题描述】:

当我单击添加到购物车的按钮时,我将创建一个会话并存储在类文件中。之后使用 gridview 显示。任何人都可以给我一个简单的概念。我几乎放弃这样做,因为我不知道。我要添加到数据库lolz。当我单击按钮添加到购物车时,代码是否正确?因为我是 asp.net 的新手。如何在gridview上显示?

我的类文件出错了

 /*missiong partial modifier on declaration of type'E-commerce_shoppingCart'; 
   another partial declaration of this type exists
public class ShoppingCart
{
    //Consists of a Price (the sum of all of your items)
    public decimal Total { get { return Items.Sum(i => i.Subtotal); } }
    //The total number of items in your cart
    public int TotalItems { get { return Items.Count; } }

    //A Collection of "Cart Items"
    public List<CartItem> Items { get; set; }

    //Basic constructor
    public ShoppingCart()
    {
        Items = new List<CartItem>();
    }
}

//A Shopping Cart Item
public class CartItem
{
    public string ItemID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
    public int Quantity { get; set; }

    //Returns the subtotal (price * quantity)
    public decimal Subtotal { get { return Price * Quantity; } }

    public CartItem()
    {

    }

    //Constructor that accepts your items
    public CartItem(string id, string name, decimal price, int quantity)
    {
        ItemID = id;
        Name = name;
        Price = price;
        Quantity = quantity;
    }
}

在我的产品详细信息页面我应该如何创建一个会话,并一个一个存储,因为有一些 contitions

if (Session["Cart"] == null)
                {
                    Session["Cart"] = new ShoppingCart();
                }


                ShoppingCart cart = Session["Cart"] as ShoppingCart;


   //below correct? need to put else? how to store one by one           
 cart.Items.Add(new CartItem(Id, lblName.Text, Decimal.Parse(lblPrice.Text), int.Parse(txtAddtoCart.Text)));
//how should i store at class file, my id is passing from query string, assuming now is static


                Session["Cart"] = cart;

【问题讨论】:

  • 你得到什么错误?
  • Cart.Item(string id,string name,Decimal price,int quantity) (+1 重载) 错误最佳重载方法匹配'E_Commerce.CartItem.CartItem(string, string,decimal ,int)' 有一些无效参数

标签: c# asp.net class datagridview shopping-cart


【解决方案1】:

根据您收到的错误判断,您似乎需要将 lblPrice.Text 转换为小数,并将 txtAddtoCart.Text 转换为 int。您的方法需要一个小数和一个整数,但您目前正在传递所有字符串。

Convert.ToDecimal(lblPrice.Text)
Convert.ToInt32(lbltxtAddtoCart.Text)

【讨论】:

  • cart.Items.Add(new CartItem(Id, lblName.Text, Decimal.Parse(lblPrice.Text), int.Parse(txtAddtoCart.Text)));这对吗?我如何从 gridview 中检索?
  • 这个网格视图在哪里?这是一个什么样的项目?
猜你喜欢
  • 2013-07-01
  • 1970-01-01
  • 2019-10-27
  • 1970-01-01
  • 2015-09-13
  • 2017-10-26
  • 1970-01-01
  • 1970-01-01
  • 2013-06-25
相关资源
最近更新 更多