我正在尝试从购物车中添加订单列表。
我这样做类似于http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-9
db.saveChanges() 有错误,'更新条目时发生错误。有关详细信息,请参阅内部异常。
当我看到订单时,orderId 为“0”。这有问题吗?
我给你一些代码。
你能给我一些想法吗?
谢谢。
//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; }
}
这是控制器。
//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);
}