English 中文(简体)
MVC3 如何更新多个项目的库存
原标题:MVC3 how to update stock of multiple items

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; }
}
最佳回答

::

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
}

www.un.org/spanish/ecosoc Edit: 为这一产品调整存货,仅作以下调整:

Instead of doing this snipped:

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

修改如下:

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

这从产品存量中减去订购数量。 然后,当你称之为以下线(你已在你的代码中做了改动)时,该数据库可节省改动:

db.SaveChanges();
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签