MVC3如何更新多个项目的股票(MVC3 how to update stock of multip

2019-07-29 01:10发布

我下面这个教程http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9

(不完全与此相同)

我试图控制库存。

当为了创造,我要扣除库存量(库存 - 计数(数量数));

我有3个表; 订单,订单明细和产品。

创建订单明细列表后,我想修改产品的库存。

我不知道如何修改股票? 我可以使用像 '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;
    }

的ViewModels

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; }
}

Answer 1:

这个片断:

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();

要创建一个新产品之前做一只股票检查,你应该确保你有一定的左边,如果没有,做一些事情,这样的事情:

if (item.Product.stock - item.Count <= 0) 
{
    //you're out of stock! do something about it here? 
}
else
{
    //do your code above to create the new Product
}

编辑:只是调整的股票该产品,只需做到以下几点:

而不是做的这个剪断:

var productStock = new Product
        {   //Is it right coding??
            stock = item.Product.stock - item.count
        };

将其更改为:

item.Product.stock = item.Product.stock - item.count;

这会减去产品库存编号有序数。 然后当你调用以下行(你已经这样做在你的代码),将更改保存到数据库:

db.SaveChanges();


文章来源: MVC3 how to update stock of multiple items