开发者

Total not updating on Database in MvcMusicStore Tutorial? MVC2

I am using MVC2 and I am following the tutorial on MvcMusicStore. Th开发者_如何转开发e problem is that the total does not update in the Orders table in the database. Every time I order something from the site and then go onto the database the Total field still says 0.00. Has anyone figured this out. Below is the code:

public int CreateOrder(Order order)
{
    decimal totalOrder = GetTotal();

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

        cricket_Model.OrderDetails.AddObject(orderDetails);
        totalOrder = (cartItem.Count * cartItem.Product.Price);
    }

    // Set the order's total to the orderTotal count
    order.Total = totalOrder;

    //Save the order
    cricket_Model.SaveChanges();

    //Empty the shopping cart
    EmptyCart();

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

I just want to say that everything updates in the OrderDetails table and everything updates in the Order table except the Total field.


I am not an expert at PERSISTENCE. But this one tickled my hair follicles for a good two hours. The work around for that is as was posted by @Ben Pretorius

//Save Order
storeDB.Orders.Add(order);
storeDB.SaveChanges();

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

//! ADD THIS METHOD TO PERSIST DATA AGAIN! And your Total will Update.
storeDB.SaveChanges();

Although we are saving the DB in the CreateOrder Method in ShoppingCart, it still has Order property as a null item.

Whereas when you are returning the context of the page to the AddressAndPayement ActionResult, the Order property of the Cart is complete. Therefore when you make a save to the DB in the Controller it saves the Order Total.

I am still trying to get it to update from the Method within. Anyone else have a better idea?


This tickled my brain aswell! I am using MVC3 and find myself writing a custom e-commerce system based roughly on this tutorial as I am new to MVC3. I Experienced the same problem where it Updates my order table as well as order detail table but not the total column of the Order table.

The solution I came up with is as Follows:

Inside your CheckoutController.cs make the folowing change and add storeDB.SaveChanges(); after the CreateCard(order); Method as per example below:

    // POST: /Checkout/AddressAndPayment

    [HttpPost]
    public ActionResult AddressAndPayment(FormCollection values)
    {
        var order = new Order();
        TryUpdateModel(order);

        try
        {
            if (string.Equals(values["PromoCode"], PromoCode,
                StringComparison.OrdinalIgnoreCase) == false)
            {
                return View(order);
            }
            else
            {
                order.Username = User.Identity.Name;
                order.OrderDate = DateTime.Now;

                //Save Order
                storeDB.Orders.Add(order);
                storeDB.SaveChanges();

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

                //! ADD THIS METHOD TO PERSIST DATA AGAIN! And your Total will Update.
                storeDB.SaveChanges();

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

        }
        catch
        {
            //Invalid - redisplay with errors
            return View(order);
        }
    }

This seems a bit Dodgy/Strange.... If i add a breakpoint to the SaveChanges inside the ShoppingCartModel then it also persist the Data but if you check the DB Afterwards it still shows the total as Null... The above code however fixes the issue...


totalOrder should be (note the +=) in the loop,

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

Also it doesn't seem like you are saving the changes that totalOrder is a part of to the database. Because you don't show us where totalOrder is defined, it is hard to tell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜