AddEditPage.xaml.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. namespace KarpovTur
  16. {
  17. /// <summary>
  18. /// Логика взаимодействия для AddEditPage.xaml
  19. /// </summary>
  20. public partial class AddEditPage : Page
  21. {
  22. private Hotel _currentHotel = new Hotel();
  23. public AddEditPage(Hotel selectedHotel)
  24. {
  25. InitializeComponent();
  26. if (selectedHotel != null)
  27. _currentHotel = selectedHotel;
  28. DataContext = _currentHotel;
  29. ComboCountries.ItemsSource = KarpovTuriEntities1.GetContext().Country.ToList();
  30. }
  31. //gfdg
  32. private void BtnSave_Click(object sender, RoutedEventArgs e)
  33. {
  34. StringBuilder errors = new StringBuilder();
  35. if (string.IsNullOrWhiteSpace(_currentHotel.Name))
  36. errors.AppendLine("Укажите название отеля");
  37. if (_currentHotel.CountOfStars < 1 || _currentHotel.CountOfStars > 5)
  38. errors.AppendLine("Количество звёзд от 1 до 5");
  39. if (_currentHotel.Country == null)
  40. errors.AppendLine("Выбирете страну");
  41. if (errors.Length>0)
  42. {
  43. MessageBox.Show(errors.ToString());
  44. return;
  45. }
  46. if (_currentHotel.id_hotel == 0)
  47. KarpovTuriEntities1.GetContext().Hotel.Add(_currentHotel);
  48. try
  49. {
  50. KarpovTuriEntities1.GetContext().SaveChanges();
  51. MessageBox.Show("Сохранено!");
  52. Manager.MainFrame.GoBack();
  53. }
  54. catch (Exception ex)
  55. {
  56. MessageBox.Show(ex.Message.ToString());
  57. }
  58. }
  59. }
  60. }