public ActionResult ProcedToCheckout(CreateOrderViewModel viewModel) { var customer = orderRepo.GetSingleEntity(x => x.UserId == User.Identity.Name); if (ModelState.IsValid) { Order order = ConvertViewModelToOrder(viewModel, User.Identity.Name); orderRepo.Insert(order); orderRepo.SaveChanges(); shoppingCartLogic = ShoppingCartLogic.GetShoppingCart(this.HttpContext); order.TotalPrice = shoppingCartLogic.ShoppingCartToOrderDetails(order); orderRepo.Update(order); orderRepo.SaveChanges(); return RedirectToAction("Index"); } viewModel.PaymentMethods = paymentMethodRepo.GetWithFilterAndOrder(); viewModel.CollectionMethods = collectionRepo.GetWithFilterAndOrder(); return View(viewModel); }
/// <summary> /// 把ProcedToCheckout()傳回來的CreateOrderViewModel轉成對應的Order /// 因為Order可能是「送餐到府」也有可能是「來店取餐」,所以需要做到判斷 /// </summary> /// <param name="viewModel"></param> /// <param name="userId"></param> /// <returns></returns> private Order ConvertViewModelToOrder(CreateOrderViewModel viewModel, string userId) { Order order = new Order() { UserId = userId, OrderDateTime = DateTime.Now, RequireDateTime = viewModel.RequireDateTime, TotalPrice = 0, PaymentMethodId = viewModel.PaymentMethodId, CollectionMethodId = viewModel.CollectionMethodId, Comment = viewModel.Comment }; if ((viewModel.PaymentMethodId == 1) && (viewModel.IsUserRegAddress == false)) { order.Address_AddCity = viewModel.AddCity; order.Address_AddDistrict = viewModel.AddDistrict; order.Address_AddFull = viewModel.AddFull; order.Address_PostCode = viewModel.PostCode; } else { Customer customer = customerRepo.GetSingleEntity(x => x.UserId == User.Identity.Name); order.Address_AddCity = customer.Address.AddCity; order.Address_AddDistrict = customer.Address.AddDistrict; order.Address_AddFull = customer.Address.AddFull; order.Address_PostCode = customer.Address.PostCode; } return order; }
//private EOSystemContex db = new EOSystemContex(); public ActionResult ProcedToCheckout() { var viewModel = new CreateOrderViewModel() { RequireDateTime = DateTime.Now, PaymentMethods = paymentMethodRepo.GetWithFilterAndOrder(), CollectionMethods = collectionRepo.GetWithFilterAndOrder() }; return View(viewModel); }