MVC3如何更新多个项目的股票 [英] MVC3 how to update stock of multiple items

查看:104
本文介绍了MVC3如何更新多个项目的股票的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

(不完全一样,因为这)

我试图控制库存。

在顺序创建,我要扣除库存量(库存 - 计(数字量));

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

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

我不知道如何修改股票?我可以使用像'db.OrderDetails.Add(的OrderDetail)'?

你能帮助我吗?我给你一些编码。

请咨询我。谢谢!

 公众诠释CreateOrder(订单顺序)
    {
        小数的OrderTotal = 0;        变种cartItems = GetCartItems();        //迭代购物车中的商品,增加每个订单详细信息
        的foreach(在cartItems VAR项)
        {
            VAR的OrderDetail =新订单明细
            {
                productId参数= item.productId,
                订单ID = order.orderId,
                单价= item.priceValue,
                rentalPeriod = item.rentalPeriod,
                的startDate = item.dateCreated.AddDays(2),
                结束日期= item.dateCreated.AddDays(2 + item.rentalPeriod),
                数量= item.count
            };
            VAR productStock =新产品
            {//是正确的编码?
                股票= item.Product.stock - item.count
            };            //订单总额的购物车设置
            的OrderTotal + =(item.count * item.priceValue);            //可能是我们需要在这里一些编码的库存控制。
            db.OrderDetails.Add(的OrderDetail);            db.SaveChanges();
        }        //设置订单的总对数的OrderTotal
        order.total =的OrderTotal;        //保存顺序
        db.SaveChanges();        //清空购物车
        空购物车();        //返回的OrderId作为确认号码
        返回order.orderId;
    }

的ViewModels

 公共类ShoppingCartViewModel
{
    公开名单<购物车和GT; CartItems {搞定;组; }
    公共小数CartTotal {搞定;组; }
}

订单明细

 公共类订单明细
{
    [关键词]公众诠释orderDetailId {搞定;组; }
    公众诠释的orderId {搞定;组; }
    公众诠释的productId {搞定;组; }
    公众诠释数量{搞定;组; }
    公共小数单价{搞定;组; }
    公众诠释rentalPeriod {搞定;组; }
    公众的DateTime的startDate {搞定;组; }
    公共DateTime的结束日期{搞定;组; }
    公共虚拟产品产品{搞定;组; }
    公共虚拟订购订购{搞定;组; }
}

订单

 公共类订单
{
    rentalDB DB =新rentalDB();    [关键词]公众诠释的orderId {搞定;组; }
    公众诠释客户ID {搞定;组; }
    公共小数总{搞定;组; }
    公共虚拟客户客户{搞定;组; }
    公开名单<&订单明细GT;订单明细{搞定;组; }
}

 公共类车
{
    [键]
    公众诠释的recordId {搞定;组; }
    公共字符串cartId {搞定;组; }
    公众诠释的productId {搞定;组; }
    公共小数priceValue {搞定;组; }
    公众诠释计数{搞定;组; }
    公众诠释rentalPeriod {搞定;组; }
    公众的DateTime dateCreated会获得{;组; }
    公共虚拟产品产品{搞定;组; }
}


解决方案

这个片断:

  VAR productStock =新产品
        {//是正确的编码?
            股票= item.Product.stock - item.count
        };        //订单总额的购物车设置
        的OrderTotal + =(item.count * item.priceValue);        //可能是我们需要在这里一些编码的库存控制。
        db.OrderDetails.Add(的OrderDetail);        db.SaveChanges();

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

 如果(item.Product.stock  -  item.Count< = 0)
{
    //你脱销!做点什么吗?
}
其他
{
    //上面做您的code创建新产品
}

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

而不是做这个剪断:

  VAR productStock =新产品
        {//是正确的编码?
            股票= item.Product.stock - item.count
        };

将其更改为:

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

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

  db.SaveChanges();

I am following this tutorial http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9

(not exactly same as this)

I am trying to control the stock.

When order created, I want to deduct amount of stock (Stock - count(number of quantity));

I have 3 tables; Order, OrderDetails, and Product.

After creating OrderDetails list, I want to modify the stock of product.

I don't know how to modify stock? Can I use like 'db.OrderDetails.Add(orderDetail)'?

Could you help me? I am giving you some coding.

Please advice me. Thanks!

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

OrderDetails

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

Order

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

Cart

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

解决方案

This snippet:

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

To do a stock check before creating a new product, you should probably make sure you have some left, if not, do something about it, something like this:

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
}

Edit: To just adjust the stock for that product, just do the following:

Instead of doing this snipped:

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

Change it to:

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

That'll subtract the ordered number from the Product stock number. Then when you call the following line (which you already do in your code), the changes are saved to the database:

db.SaveChanges();

这篇关于MVC3如何更新多个项目的股票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆