购物车类
public int AddToCart(Product product)
{
检查是否有可用的股票,并且是连接到我要返回视图的其他的 if 语句
if (product.productStock < 1)
{
// Get the matching cart and item instances
var cartItem = storeDB.Carts.SingleOrDefault(
c => c.CartId == ShoppingCartId
&& c.ProductId == product.productId);
if (cartItem == null)
{
// Create a new cart item if no cart item exists
cartItem = new Cart
{
ProductId = product.productId,
CartId = ShoppingCartId,
Count = 1,
DateCreated = DateTime.Now,
CPName = product.productName,
CPPrice = product.Price
};
storeDB.Carts.Add(cartItem);
//1 will be taken from the total product stock
product.productStock = product.productStock - 1;
}
else
{
// If the item does exist in the cart,
// then add one to the quantity
cartItem.Count++;
//1 will be taken from the total product stock
product.productStock = product.productStock - 1;
}
// Save changes
storeDB.SaveChanges();
return cartItem.Count;
}
else
{
尝试使其返回以下视图
return ...
}
}
缺货视图
此视图存储在购物车文件夹中
@{
ViewBag.Title = "outofstock";
}
<h2>Sorry this item is out of stock please select another</h2>
<button type="submit" class="btn btn-primary" onclick="location.href='@Url.Action("Index", "Home")'">Return to products page</button>
使用重定向功能将用户带到您选择的 URL
Redirect("http://www.yourdomain.com/page");