2

私はこのチュートリアルに従ってい ますhttp://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9

(これとまったく同じではありません)

在庫を管理しようとしています。

注文が作成されたときに、在庫の量を差し引きたい(在庫-カウント(数量));

私は3つのテーブルを持っています。Order、OrderDetails、およびProduct。

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

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

1 に答える 1

2

このスニペット:

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();
于 2012-04-17T19:08:37.167 に答える