问题描述
我希望能够理解如何使用CartSessionKey变量。
我将cartId键放在会话变量CartSessionKey中但我找不到如何将
提取回代码中以便能够检查产品表数量 -
购物车数量。
感谢您的解释。
这是我的ShoppingCart.cs
/ /我们正在使用HttpContextBase来访问cookie。
公共字符串GetCartId(HttpContextBase httpcontext)
{
if(HttpContext.Current.Session [CartSessionKey] == null)
{
if(!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name) )
{
HttpContext.Current.Session [CartSessionKey] =
HttpContext.Current.User.Identity.Name;
}
其他
{
//使用System.Guid类生成一个新的随机GUID
Guid tempCartId = Guid.NewGuid();
//将tempCartId作为cookie发送回客户端
HttpContext.Current.Session [CartSessionKey] = tempCartId.ToString();
}
}
返回HttpContext.Current.Session [CartSessionKey] .ToString();
}
这里是PanierController.cs
I would like to be able to understand how to used the CartSessionKey variable.
I put the cartId key in the session variable CartSessionKey but I can't find out how
to extract it back in the code to be able to check for the Product table quantity -
the cart quantity.
thanks for your explanation.
Here is my ShoppingCart.cs
// We're using HttpContextBase to allow access to cookies.
public string GetCartId(HttpContextBase httpcontext)
{
if (HttpContext.Current.Session[CartSessionKey] == null)
{
if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name))
{
HttpContext.Current.Session[CartSessionKey] =
HttpContext.Current.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
HttpContext.Current.Session[CartSessionKey] = tempCartId.ToString();
}
}
return HttpContext.Current.Session[CartSessionKey].ToString();
}
And here is the PanierController.cs
public ActionResult AddToCart(int id)
{
// Retrieve the album from the database
var prd = dbProduit.Produits.Single(produit => produit.ProduitId == id);
if (prd.Quantite - pan.Quantite <= 0)
{
/* err.message = "Erreur la quantite depasse celle de l'inventaire du produit, veuillez choisir un autre produit"; */
TempData["err_message"] = "Erreur la quantite depasse celle de l'inventaire du produit, veuillez choisir un autre produit";
}
推荐答案
// We're using HttpContextBase to allow access to cookies.
public string GetCartId(HttpContextBase httpcontext)
{
if (HttpContext.Current.Session["CartSessionKey"] == null)
{
if (!string.IsNullOrWhiteSpace(HttpContext.Current.User.Identity.Name))
{
HttpContext.Current.Session["CartSessionKey"] =
HttpContext.Current.User.Identity.Name;
}
else
{
// Generate a new random GUID using System.Guid class
Guid tempCartId = Guid.NewGuid();
// Send tempCartId back to client as a cookie
HttpContext.Current.Session["CartSessionKey"] = tempCartId.ToString();
}
}
return HttpContext.Current.Session["CartSessionKey"].ToString();
}
这篇关于如何处理CartSessionKey变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!