我正在关注本教程 http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9
(与此不完全相同)
我正在努力控制库存。
创建订单时,我想扣除库存数量(库存 - 计数(数量));
我有 3 张桌子;订单、订单详情和产品。
创建 OrderDetails 列表后,我想修改产品的库存。
不知道怎么修改库存?我可以像 'db.OrderDetails.Add(orderDetail)' 这样使用吗?
你可以帮帮我吗?我给你一些编码。
请给我建议。谢谢!
public int CreateOrder(Order order)
{
decimal orderTotal = 0;
var cartItems = GetCartItems();
// Iterate over the items in the cart, adding the order details for each
foreach (var item in cartItems)
{
var orderDetail = new OrderDetails
{
productId = item.productId,
orderId = order.orderId,
unitPrice = item.priceValue,
rentalPeriod = item.rentalPeriod,
startDate = item.dateCreated.AddDays(2),
endDate = item.dateCreated.AddDays(2 + item.rentalPeriod),
quantity = item.count
};
var productStock = new Product
{ //Is it right coding??
stock = item.Product.stock - item.count
};
// Set the order total of the shopping cart
orderTotal += (item.count * item.priceValue);
//Could be we need some coding here for stock control.
db.OrderDetails.Add(orderDetail);
db.SaveChanges();
}
// Set the order's total to the orderTotal count
order.total = orderTotal;
// Save the order
db.SaveChanges();
// Empty the shopping cart
EmptyCart();
// Return the OrderId as the confirmation number
return order.orderId;
}
视图模型
public class ShoppingCartViewModel
{
public List<Cart> CartItems { get; set; }
public decimal CartTotal { get; set; }
}
订单详细信息
public class OrderDetails
{
[Key] public int orderDetailId { get; set; }
public int orderId { get; set; }
public int productId { get; set; }
public int quantity { get; set; }
public decimal unitPrice { get; set; }
public int rentalPeriod { get; set; }
public DateTime startDate { get; set; }
public DateTime endDate { get; set; }
public virtual Product Product { get; set; }
public virtual Order Order { get; set; }
}
命令
public class Order
{
rentalDB db = new rentalDB();
[Key] public int orderId { get; set; }
public int customerId { get; set; }
public decimal total { get; set; }
public virtual Customer Customer { get; set; }
public List<OrderDetails> OrderDetails { get; set; }
}
大车
public class Cart
{
[Key]
public int recordId { get; set; }
public string cartId { get; set; }
public int productId { get; set; }
public decimal priceValue { get; set; }
public int count { get; set; }
public int rentalPeriod { get; set; }
public DateTime dateCreated { get; set; }
public virtual Product Product { get; set; }
}