public ActionResult Checkout(CheckoutViewmodel viewmodel) { if (!ModelState.IsValid) { return(View("Checkout")); } var listCartItem = (List <CartItem>)Session["ShoppingCart"]; if (listCartItem == null) { return(RedirectToAction("Index", "Error")); } else { double total = 0; var customer = new Customer { Name = viewmodel.Name, Email = viewmodel.Email, PhoneNumber = viewmodel.PhoneNumber, Address = viewmodel.Address }; var order = new Order { CustomerId = customer.Id, Status = false, DateTime = DateTime.Now }; foreach (var item in listCartItem) { var orderDetail = new OrderDetail { OrderId = order.Id, ProductId = item.Product.Id, Price = item.Product.Price, Quantity = item.Quantity }; _dbContext.OrderDetails.Add(orderDetail); total += (item.Quantity * item.Product.Price); } order.Total = total; _dbContext.Customers.Add(customer); _dbContext.Orders.Add(order); _dbContext.SaveChanges(); Session.Remove("ShoppingCart"); return(RedirectToAction("ThankYou")); } }
public ActionResult Checkout() { CheckoutViewmodel model = new CheckoutViewmodel(); var CartproductsCookie = Request.Cookies["CartProducts"]; if (CartproductsCookie != null && !string.IsNullOrEmpty(CartproductsCookie.Value)) { //var productIDs = CartproductsCookie.Value; //var ids = productIDs.Split('-'); //List<int> ProductIDs = ids.Select(x => int.Parse(x)).ToList(); model.CartProductIDs = CartproductsCookie.Value.Split('-').Select(x => int.Parse(x)).ToList(); model.CartProducts = ProductService.Instance.GetCartProducts(model.CartProductIDs); model.User = UserManager.FindById(User.Identity.GetUserId()); } return(View(model)); }