尝试按标签设置手风琴。使用我的代码会发生一些毛刺(如下所示)。
1)第一个标签中的数据出现但是如果我切换到另一个标签,然后回到第一个标签,则它不显示正确的手风琴。
2)第四个标签甚至没有显示其手风琴
public ActionResult AddToCart(int productId)
{
// get a cart from session or create new
var cart = Session["cart"] as List<CartVM> ?? new List<CartVM>();
using(var db = new Db())
{
// get product in question
var product = db.Products
.FirstOrDefault(x => x.Id == productId)
// if this is unknown product, throw exception
if(product == null)
{
thrown new ArgumentException("Invalid product", nameof(productId));
}
// get existing line
var existingCartLine = cart
.FirstOrDefault(x => x.ProductId == productId);
// if there was no existing line, add a new one
if(existingCartLine == null)
{
cart.Add(new CartVM
{
ProductId = product.Id,
Quantity = 1,
Price = product.Price,
TotalPrice = product.Price
});
}
else
{
// otherwise modify existing line
existingCartLine.Quantity++;
existingCartLine.TotalPrice = cartLine.Price * cartLine.Quantity;
}
}
// save cart back in the session
Session["cart"] = cart;
// return view
return PartialView(cart);
}