我正在尝试提取cartId以便能够获得cart.quantity 基于CartSessionKey。我尝试了许多不同的方法,但它没有用。
这是我得到的错误:当前上下文中不存在名称'CartSessionKey'
我认为Session变量很容易正常使用。
感谢您提出的任何暗示。
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();
}
PanierController,CS
public ActionResult AddToCart(int id)
{
// Retrieve the album from the database
var prd = dbProduct.Products.Single(product => product.ProductId == id);
System.Web.HttpContext.Current.Session[CartSessionKey].ToString() <== error here
"missing code here to retrieve the cartId and then the cart.quantity based on
CartSessionKey"
if (prd.Quantity - cart.Quantity <= 0)
{
message = 'you're out of stock
}
else
....
我试过这个但是我在CartSessionKey下遇到错误在当前上下文中不存在
var keyCart = System.Web.HttpContext.Current.Session [CartSessionKey] .ToString();