//new project start with OnGet() //here's our first custom method to Post. public async Task <IActionResult> OnPost() { if (ModelState.IsValid == false) //ie, if something is wrong with the model { return(Page()); //just return the same page. } var food = await _foodData.GetFood(); Order.Total = Order.Quantity * food.Where(x => x.Id == Order.FoodId).First().Price; int id = await _orderData.CreateOrder(Order); //fyi when you ran this program the first time, you watched the id //increase from 0 to 1 to 2 with each new order, just like your //Common project code is supposed to. gj. //return RedirectToPage("./Create"); //you can redirect anywhere you want. //note RediretToPage is for Razor Pages. RedirectToAction is for MVC. //later, changed to: return(RedirectToPage("./Display", new { Id = id })); //That Id = id is an anonymous object. Those are types without names //and that aren't created until just when you need them. //Meant to be temporary. //In this case, we create it only to pass along the property ID //to the /Display page, which needs an id to work. //So whatever we created as id here will become Id there. }
public async Task <IActionResult> OnPost() { int id; // means model return has something wrong, we should not process it if (ModelState.IsValid == false) { return(Page()); } //todo make the db call get just the food item instead of getting all food var foods = await _foodData.GetFood(); // one way to iterate //foods.ForEach(x => //{ // if (x.Id == Order.FoodID) // Order.Total = Order.Quantity * x.Price; //}); // a nicer linq way try { Order.Total = Order.Quantity * foods.Where(x => x.Id == Order.FoodID).First().Price; //save order into db id = await _orderData.CreateOrder(Order); } catch { return(RedirectToPage("./Create")); } return(RedirectToPage("./Display", new { Id = id })); }
public async Task <IActionResult> Post(OrderModel order) { var food = await _foodData.GetFood(); order.Total = order.Quantity * food.Where(x => x.Id == order.FoodId).First().Price; int id = await _orderData.CreateOrder(order); return(Ok(new { Id = id })); }
private async Task <int> PlaceOrder() { var food = await _foodData.GetFood(); Order.Total = Order.Quantity * food.Where(x => x.Id == Order.FoodId).First().Price; var orderDto = _mapper.Map <OrderModel>(Order); return(await _orderData.CreateOrder(orderDto)); }
public async Task <IActionResult> Create(OrderModel order) { if (ModelState.IsValid == false) { return(View()); } var food = await _foodData.GetFood(); order.Total = order.Quantity * food.Where(x => x.Id == order.FoodId).First().Price; int id = await _orderData.CreateOrder(order); return(RedirectToAction("Display", new { id })); }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid == false) { return(Page()); } var food = await foodData.GetFood(); Order.Total = Order.Quantity * food.Where(x => x.Id == Order.FoodId).First().Price; int id = await orderData.CreateOrder(Order); return(RedirectToPage("./Display", new { Id = id })); }
public IActionResult Checkout(Order order) { var items = _shoppingCart.GetShoppingCartItems(); _shoppingCart.ShoppingCartItems = items; if (_shoppingCart.ShoppingCartItems.Count == 0) { ModelState.AddModelError("", "Your cart is empty, add some pies first"); } if (ModelState.IsValid) { _order.CreateOrder(order); _shoppingCart.ClearCart(); return(RedirectToAction("CheckoutComplete")); } return(View(order)); }
public async Task <IActionResult> OnPost() { if (ModelState.IsValid == false) { return(Page()); } var food = await _foodData.GetFood(); // normall want to get food by id // but we have a small list Order.Total = Order.Quantity * food.Where(x => x.Id == Order.FoodId).First().Price; int id = await _orderData.CreateOrder(Order); Console.WriteLine("Order Created"); // Pass id over to the display as Id return(RedirectToPage("./Display", new { Id = id })); }
public async Task <IActionResult> OnPost() { Memberships = membershipData.GetMemberships(); ApplicationUser = await userManager.GetUserAsync(User); if (ModelState.IsValid) { foreach (var cartItem in HttpContext.Session.GetObjectFromJson <List <ShoppingCart> >("CartItems").ToList()) { OrderDetails.Add(new OrderDetail { Product = productData.GetProductById(cartItem.Id), Quantity = cartItem.Quantity }); // Product = productData.GetProductById(cartItem.Id); Product.Quantity -= cartItem.Quantity; // } if (Selected != null) { var membership = membershipData.GetMembershipById(Convert.ToInt32(Selected)); ApplicationUser.Membership = membership; Order.OrderTotal = cartBL.MemberOnCheckout(Convert.ToDouble(HttpContext.Session.GetString("TotalPrice")), ApplicationUser); } else { Order.OrderTotal = Convert.ToDouble(HttpContext.Session.GetString("TotalPrice")); } Order.OrderDetails = OrderDetails; Order.UserId = ApplicationUser.Id; // Order.DateOrdered = DateTime.Now; // orderData.CreateOrder(Order); orderData.Commit(); HttpContext.Session.Remove("CartItems"); return(RedirectToPage("/Order/CheckoutComplete")); } return(Page()); }