Help me! MVC music store v0.8

Nov 19, 2010 at 4:04 PM

Sorry for asking a foolish question !

I am developing MVC music store v0.8 (code was downloaded long time ago) . Everything's working good but the problem has occurred when i'm checking out,

Even though it's completed, the Quantity = 0 in OderDetail table, Total =0.00 in Oder table

 

Please help me out, Any suggestion would be thankful !

 

Lucas

Nov 23, 2010 at 4:31 AM

I'm also developing an MVC 2 application using the Music Store(v0.8) tutorial. I have everything completed and functioning and I made a purchase from the store and completed the checkout process, yet when the order is completed the column (Total) in the database table (Order) never gets updated as well as the Quantity column from the Order table.

I was wondering if some changes need to be made to the ShoppingCart class?

Source:

 public int CreateOrder(Order order)
        {
            decimal orderTotal = 0;
           
            var cartItems = GetCartItems();

            //Iterate the items in the cart, adding Order Details for each
            foreach (var cartItem in cartItems)
            {
                var orderDetails = new OrderDetail
                {
                    ProductID = cartItem.ProductID,
                    OrderId = order.OrderId,
                    UnitPrice = cartItem.Product.Price
                };

                storeDB.OrderDetails.AddObject(orderDetails);

                orderTotal += (cartItem.Count * cartItem.Product.Price);

                //Say maybe add this?

                orderTotal = order.Total;
            }

            //Save the order
            storeDB.SaveChanges();

            //Empty the shopping cart
            EmptyCart();

            //Return the OrderId as a confirmation number
            return order.OrderId;
        }

I'm wondering if there would be more to add or even if this is the right file to edit. Anyone have an idea of why the table is not updating?

Thanks

qpierce

 

Dec 2, 2010 at 3:33 AM

I ran the Music Store(v1.0) sample application and did the checkout process. The quantity in the OrderDetail table and the total in the Order table are not updating in this version as well. I was wondering if anyone else has this problem?

qpierce

Dec 2, 2010 at 6:08 AM

Well I figured out why the quantity wasn't updating.

Sample code: (ShoppingCart.cs)

public int CreateOrder(Order order)
        {
            decimal orderTotal = 0;

            var cartItems = GetCartItems();

            //Iterate the items in the cart, adding Order Details for each
            foreach (var cartItem in cartItems)
            {
                var orderDetails = new OrderDetail
                {
                    AlbumId = cartItem.AlbumId,
                    OrderId = order.OrderId,
                    UnitPrice = cartItem.Album.Price
                };

                storeDB.OrderDetails.AddObject(orderDetails);

                orderTotal += (cartItem.Count * cartItem.Album.Price);
            }

            //Save the order
            storeDB.SaveChanges();

            //Empty the shopping cart
            EmptyCart();

            //Return the OrderId as a confirmation number
            return order.OrderId;
        }

My code: (ShoppingCart.cs)

 public int CreateOrder(Order order)
        {
            decimal orderTotal = 0;
           
            var cartItems = GetCartItems();

            //Iterate the items in the cart, adding Order Details for each
            foreach (var cartItem in cartItems)
            {
                var orderDetails = new OrderDetail
                {
                    ProductID = cartItem.ProductID,
                    OrderId = order.OrderId,
                    UnitPrice = cartItem.Product.Price,
                    Quantity = cartItem.Count, // Needed this in order to update the Quantity column in the OrderDetail table
                   
                };
                storeDB.OrderDetails.AddObject(orderDetails);

                // I tried this. Yet the Total column is still not updating...
                orderTotal += (cartItem.Count * cartItem.Product.Price);
        
                order.Total += orderTotal;
   
            }

            //Save the order
            storeDB.SaveChanges();

            //Empty the shopping cart
            EmptyCart();

            //Return the OrderId as a confirmation number
            return order.OrderId;
        }

I'll soon figure out why the Total column is still not updating. Let me know if any of you have found a solution to this yet.

Thanks

qpierce

 

 

Jul 6, 2011 at 1:21 PM

I have found to get the Total to update in the Order table you need to add storeDB.SaveChanges(); just after cart.CreateOrder(order); in CheckoutController.cs e.g.

                    //Process the order
                    var cart = ShoppingCart.GetCart(this.HttpContext);
                    cart.CreateOrder(order);
                    storeDB.SaveChanges();

                    return RedirectToAction("Complete",
                        new { id = order.OrderId });