public ActionResult PlaceOrder(IFormCollection viewCollection, ViewModel myModel, Users user) { //int userid = int.Parse(viewCollection["UseridTD"].ToString()); //TempData["result"] = userid; int UseridTD = int.Parse(TempData.Peek("userid").ToString()); int LocationTD = int.Parse(TempData.Peek("locationid").ToString()); string NameTD = TempData.Peek("firstname").ToString(); string LastnameTD = TempData.Peek("lastname").ToString(); string PhoneTD = TempData.Peek("phone").ToString(); PizzaModel newPizza = new PizzaModel(); OrderModel order = new OrderModel(); ViewData["msg"] = "Add a Pizza"; if (Count == 11) { ViewData["msg"] = "You can create only one more pizza, (max of 12 pizzas per order)"; } else if (Count == 12) { ViewData["msg"] = "You have reach your maximun of pizzas per order (12 pizzas), Please place your order "; return(RedirectToAction(nameof(PlaceOrder))); } //ViewModel myModel = new ViewModel(); newPizza.Crust = int.Parse(viewCollection["SelectedCrust"].ToString()); newPizza.Size = viewCollection["SelectedSize"]; newPizza.Name = viewCollection["SelectedPizza"]; string sauce = viewCollection["SelectedSauce"]; // order.LocationId = user.LocationId; order.LocationId = user.LocationId; order.OrderTotal = 0; order.UsersId = user.UsersId; var toppings = new List <string>(); toppings.Add(newPizza.Name); Pizzas pizza = new Pizzas { Crust = newPizza.Crust, Size = newPizza.Size, Name = newPizza.Name }; //calculate price decimal S = 6, M = 8, L = 12; //quantity of topppings batch for pizza size if (pizza.Size == "S") { pizza.Price = S; Order_total = int.Parse(TempData.Peek("order_total").ToString()); Order_total += S; TempData["order_total"] = Order_total; } else if (pizza.Size == "M") { pizza.Price = M; Order_total = int.Parse(TempData.Peek("order_total").ToString()); Order_total += M; TempData["order_total"] = Order_total; } else { pizza.Price = L; Order_total = int.Parse(TempData.Peek("order_total").ToString()); Order_total += L; TempData["order_total"] = Order_total; } //check availability of ingredients for each pizza bool availability = Repo.CheckInventory(toppings, pizza, sauce, LocationTD); if (availability == true) { // take out toppings from db Repo.MinusToppings(toppings, pizza, sauce, LocationTD); //add pizza Repo.AddPizzas(pizza.Size, pizza.Price, pizza.Name, pizza.Crust); Repo.SaveChanges(); if (TempData.Peek("Count").ToString() == "1") { Count = 1; } else { Count = int.Parse(TempData.Peek("Count").ToString()); } if (Count < 2)//only if the order is new is going to be created { //add order Repo.AddOrder(UseridTD, LocationTD, Order_total); } // increment counter to know next time, that this is not a new order. Count++; TempData["Count"] = Count; //add orderPizza int?order_id = Repo.GetOrderByUserId(UseridTD); int?pizza_id = Repo.GetPizzaIdBySize(pizza.Name, pizza.Size); TempData["orderid"] = order_id; Repo.AddOrderPizza(order_id, pizza_id); } else { ViewData["msg"] = "Not enough reseources to complete your order, Please choose again..."; return(RedirectToAction(nameof(PlaceOrder))); } return(RedirectToAction(nameof(PlaceOrder))); }