如何用C#编写?你能用字典吗?
$count = 0;
if(count($_SESSION['goods']) > 0) {
$count = count($_SESSION['goods']) -1; // array start on zero.
}
$_SESSION['goods'][$count]["products_id"] = $_POST["products_id"];
$_SESSION['goods'][$count]["price"] = $_POST["price"];
$_SESSION['goods'][$count]["number"] = $_POST["number"];
最佳答案
有很多方法可以做到这一点,但这是一种简单的方法。 (此代码将需要在您的页面代码后面,因为它需要Page.Session属性)
首先,您可能希望一个Product实体存储您的数据:
[Serializable]
public class Product
{
public int ProductId{get;set;}
public int Price{get;set;}
public int Number{get;set;}
}
然后,您可以将产品存储在会话中,如下所示:
public void AddProductToSession(Product product)
{
var products = Session["goods"] as Dictionary<int, Product>;
if (products == null) products = new Dictionary<int, Product>();
products.Add(product.ProductId, product);
Session["goods"] = product;
}
public Product GetProductFromSession(int productId)
{
Product product;
var products = Session["goods"] as Dictionary<int, Product>;
if (products == null || !products.TryGetValue(productId, out product))
throw Exception(string.Format("Product {0} not in session", productId));
return product;
}
关于c# - PHP到C#.NET/ASP.NET,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1398988/