public ActionResult Create(BookingViewModel bookingView) { if (Session["LoggedCustomerId"] == null) { return(RedirectToAction("Login", "Login")); } if (ModelState.IsValid) { if (int.TryParse(Session["LoggedCustomerId"].ToString(), out int id)) { Customer customer = db.GetCustomer(id); Booking newBooking = new Booking(bookingView.StartDate, bookingView.EndDate, new List <Tool>(), customer, "Reserveret", bookingView.TotalPrice); foreach (int i in bookingView.SelectedTools) { var tool = db.Tools.Find(i); db.Entry(tool).Collection(t => t.Bookings).Load(); newBooking.Tools.Add(tool); tool.Bookings.Add(newBooking); } customer.Bookings.Add(newBooking); db.Bookings.Add(newBooking); db.SaveChanges(); return(View("BookingInformation", newBooking)); } else { return(View(bookingView)); } } else { return(View(bookingView)); } }
/** * Search for customer with an id. * If found show the data on the customer in CustomerGrid. * If not found set ErrorLabels text and show error message. */ private void FindCustomerButton_Click(object sender, RoutedEventArgs e) { // Hide labels with errors and other info for the user. HideInteractionLabels(); // Hide edit customer elements if need be. if (EditButton.Content.ToString() == "Gem Ændringer") { HideEditCustomerElements(); // Set InfoLabels text to inform the user. SetLabelGreenText(InfoLabel, "Der blev ikke gemt nogen ændringer."); } // Get text from search textbox. string result = TxtBoxCustomerId.Text; // Parse string text to int value. if (int.TryParse(result, out int id)) { // Search for customer in database. Customer customer = _context.GetCustomer(id); // If customer was found. if (customer != null) { // Show customer info in customerGrid. CustomerGrid.DataContext = customer; // Sort and show customer bookings in listbox. _context.SortBookings(customer.Bookings); BookingListBox.ItemsSource = customer.Bookings; EditButton.Visibility = Visibility.Visible; ResetSearchTxtBox(); SetLabelGreenText(SearchErrorLabel, "Succes! Kunden blev fundet."); } else { SetLabelRedText(SearchErrorLabel, "Beklager, en kunde med det angivne id blev ikke fundet."); ResetSearchTxtBox(); } } else { SetLabelRedText(SearchErrorLabel, "Indtast venligst et id bestående af tal."); ResetSearchTxtBox(); } }