public IActionResult Visit(int ID) { StoreModel store; try { store = _repo.GetStore(ID); } catch (SqlException e) { if (e.Message.Contains("server was not found")) { Console.WriteLine("Could not connect to the SQL database"); StoreViewModel thisModel = new StoreViewModel(); thisModel.ReasonForError = "An internal error has occured. Please return to the main page and try again."; return(View("Error", thisModel)); } throw e; } if (store == null) { StoreViewModel thisModel = new StoreViewModel(); thisModel.ReasonForError = $"A store with an ID of {ID} does not exist. Please enter a different ID from the URL, or select a store from the selection page after logging in."; return(View("Error", thisModel)); } List <MenuModel> items = _repo.GetMenu(ID); List <PizzaModel> pizzas = new List <PizzaModel>(); List <CheckModel> pizzasToSelectFrom = new List <CheckModel>(); List <ToppingModel> toppings = _repo.GetToppings(); foreach (MenuModel item in items) { PizzaModel pizza = _repo.GetPizza(item.PizzaID); if (pizza == null) { Console.WriteLine($"Unknown pizza found with ID {item.PizzaID} from store {item.StoreID} at menu ID {item.ID}"); continue; } string[] temp = pizza.DefaultToppings.Split(','); int[] defaultToppingIDs = new int[temp.Length]; for (int i = 0; i < temp.Length; i++) { if (!int.TryParse(temp[i], out defaultToppingIDs[i])) { Console.WriteLine($"Database error: Expected integer for default topping ID in pizza {pizza.ID}, got {temp[i]}"); continue; } } pizzas.Add(pizza); CrustModel crust = _repo.GetCrust(pizza.DefaultCrustID); ToppingViewModel[] toppingsSelected = new ToppingViewModel[toppings.Count()]; for (int i = 0; i < toppingsSelected.Length; i++) { ToppingModel topping = toppings[i]; toppingsSelected[i] = new ToppingViewModel { ID = topping.ID, Name = topping.Name, IsSelected = defaultToppingIDs.Contains(topping.ID) }; } pizzasToSelectFrom.Add(new CheckModel { ID = pizza.ID, Name = pizza.Name, Checked = false, Cost = pizza.Cost, DefaultCrust = crust.ID, SelectedCrust = crust.ID.ToString(), SelectedToppings = toppingsSelected }); } List <SelectListItem> crustDropDownOptions = new List <SelectListItem>(); foreach (CrustModel crust in _repo.GetCrusts()) { crustDropDownOptions.Add(new SelectListItem { Text = crust.Name, Value = crust.ID.ToString() }); } StoreViewModel model = new StoreViewModel(); model.StoreName = store.Name; model.Menu = pizzasToSelectFrom; try { _ = userLoggedIn; // keeps the session data alive } catch (NullReferenceException) { // people can view menus if they're not logged in, but not order } model.Crusts = crustDropDownOptions; model.Toppings = toppings; TempData["StoreID"] = store.ID; TempData.Keep("StoreID"); return(View(model)); }