http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9"rel=“nofollow” http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9
(与)
我试图控制库存。
在发出订单时,我想扣除库存量(Stock-(数量));
我有3个表格;命令、命令和产品。
在制定了《命令清单》之后,我想修改产品存量。
我不知道如何改变存量? 我可以像 d。 命令.Add(orderDetail)?
你们能否帮助我? 我给你一些.。
请向我提供咨询意见。 感谢!
public int Create命令(命令 order)
{
decimal orderTotal = 0;
var cartItems = Get第二条Items();
// Iterate over the items in the cart, adding the order details for each
foreach (var item in cartItems)
{
var orderDetail = new 命令
{
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.命令.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
Empty第二条();
// Return the 命令Id as the confirmation number
return order.orderId;
}
观点
public class Shopping第二条ViewModel
{
public List<第二条> 第二条Items { get; set; }
public decimal 第二条Total { get; set; }
}
命令
public class 命令
{
[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 命令 命令 { get; set; }
}
命令
public class 命令
{
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<命令> 命令 { get; set; }
}
第二条
public class 第二条
{
[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; }
}