0

I am trying to add order list from shopping cart.

I am doing this similarly as http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9

db.saveChanges() has error, 'An error occurred while updating the entries. See the inner exception for details.'

When I see the order, orderId is '0'. Is it problem with this?

I am giving you some code.

Could you give me some idea?

Thanks.

//Order.cs
public class Order
{
    rentalDB db = new rentalDB();

    [Key] public int orderId { get; set; }
    public int customerId { get; set; }
    public DateTime orderDate { get; set; }
    public int rentalPeriod { get; set; }
    public DateTime startDate { get; set; }
    public decimal total { get; set; }
    public virtual Customer Customer { get; set; }
    public List<OrderDetails> OrderDetails { get; set; }
}

And here is controller.

//AccountController.cs

[Authorize]
    public ActionResult Order()
    {
        var order = new Order();
        TryUpdateModel(order);

        string customerName = User.Identity.Name;
        if (customerName == "")
        {
            return RedirectToAction("LogOn", "Account");
        }
        int customerId = (from a in db.Customer
                          where a.userName == customerName
                          select a.customerId).Single();
        order.customerId = customerId;

        db.Order.Add(order);
        //I added orderDate and startDate, then it is saving now.
        order.orderDate = System.DateTime.Now;
        order.startDate = System.DateTime.Now.AddDays(2);
        db.SaveChanges();

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


        return View(order);
    }
wholee1
  • 1,491
  • 13
  • 34
  • 51

1 Answers1

0

My problem was I did not mention about orderDate and startDate.

Then I have got error.

When I declared the date, it is working now.

wholee1
  • 1,491
  • 13
  • 34
  • 51