public ActionResult Edit(string hotelId) { //find the hotel in the repository // Hotel h = hotelRepo.FindByCode(hotelId); Hotel h = hotelRepo.FindAll().FirstOrDefault(t => t.HotelId.Equals(hotelId)); //making the lists so that you only go to the Repo's once List <ContactPerson> contact = contactRepo.FindAll().OrderBy(t => t.LastName).ToList(); List <Branch> branch = branchRepo.FindAll().OrderBy(t => t.Name).ToList(); List <Owner> owner = ownerRepo.FindAll().OrderBy(t => t.LastName).ToList(); List <Status> status = statusRepo.FindAll().OrderBy(t => t.St).ToList(); //check if the hotel exist (normally this can never give null unless DB failure) if (h == null) { return(HttpNotFound()); } //Use a viewmodel to display al the details of the hotel EditHotelViewModel ehvm = new EditHotelViewModel(h) { //select the active branch SelectedBranchId = branch.Where(t => t.BranchId == h.Branch.BranchId).FirstOrDefault().Name, //fill the list with all the branches Branch = branch.Select(t => new SelectListItem { Value = t.BranchId.ToString(), Text = t.Name }), //repeat for al the other lists //Contactperson //it doesn't matter with the selectedId if you use the id of the name SelectedContactPersonId = contact.Where(t => t.ContactPersonId == h.ContactPerson.ContactPersonId).FirstOrDefault().ContactPersonId.ToString(), ContactPerson = contact.Select(t => new SelectListItem { Value = t.ContactPersonId.ToString(), Text = t.LastName + " " + t.FirstName }), //Owner SelectedOwnerId = owner.Where(t => t.OwnerId == h.Owner.OwnerId).FirstOrDefault().OwnerId.ToString(), Owner = owner.Select(t => new SelectListItem { Value = t.OwnerId.ToString(), Text = t.LastName + " " + t.FirstName }), //status SelectedStatusId = status.Where(t => t.St == h.Status.St).FirstOrDefault().ToString(), Status = status.Select(t => new SelectListItem { Value = t.St, Text = t.St }) }; return(View(ehvm)); }
public ActionResult Create() { //making the lists so that you only go to the Repo's once List <ContactPerson> contact = contactRepo.FindAll().OrderBy(t => t.LastName).ToList(); List <Branch> branch = branchRepo.FindAll().OrderBy(t => t.Name).ToList(); List <Owner> owner = ownerRepo.FindAll().OrderBy(t => t.LastName).ToList(); List <Status> status = statusRepo.FindAll().OrderBy(t => t.St).ToList(); try { Hotel hotel = new Hotel(); EditHotelViewModel ehvm = new EditHotelViewModel(hotel) { //select the default branch SelectedBranchId = branch.FirstOrDefault().BranchId.ToString(), //fill the list with all the branches Branch = branch.Select(t => new SelectListItem { Value = t.BranchId.ToString(), Text = t.Name }), //repeat for al the other lists //Contactperson SelectedContactPersonId = contact.FirstOrDefault().ContactPersonId.ToString(), ContactPerson = contact.Select(t => new SelectListItem { Value = t.ContactPersonId.ToString(), Text = t.LastName + " " + t.FirstName }), //Owner SelectedOwnerId = owner.FirstOrDefault().OwnerId.ToString(), Owner = owner.Select(t => new SelectListItem { Value = t.OwnerId.ToString(), Text = t.LastName + " " + t.FirstName }), //status SelectedStatusId = status.FirstOrDefault().ToString(), Status = status.Select(t => new SelectListItem { Value = t.St, Text = t.St }) }; return(View(ehvm)); } catch (Exception ex) { TempData["error"] = "Somethign whent wrong. Please contact the IT department"; return(RedirectToAction("Index")); } }
private void MapToHotel(EditHotelViewModel ehvm, Hotel hotel, Context x) { int br = (Convert.ToInt32(ehvm.SelectedBranchId)); int cp = (Convert.ToInt32(ehvm.SelectedContactPersonId)); int ow = (Convert.ToInt32(ehvm.SelectedOwnerId)); hotel.Branch = x.Branches.FirstOrDefault(t => t.BranchId == br); hotel.ContactPerson = x.ContactPersons.FirstOrDefault(t => t.ContactPersonId == cp); hotel.Email = ehvm.Email; hotel.Owner = x.Owner.FirstOrDefault(t => t.OwnerId == ow); hotel.TelephoneNumber = ehvm.TelephoneNumber; hotel.VatNumber = ehvm.VatNumber; hotel.Adres = ehvm.Adres; hotel.HotelId = ehvm.HotelId; hotel.Name = ehvm.Name; hotel.Status = x.Statusses.FirstOrDefault(t => t.St == ehvm.SelectedStatusId); }
public ActionResult Create(EditHotelViewModel ehvm) { //if (ModelState.IsValid) { try { Hotel h = new Hotel(); hotelRepo.AddHotel(h); Context x = hotelRepo.getContext(); MapToHotel(ehvm, h, x); x.SaveChanges(); TempData["message"] = String.Format("Hotel {0} werd gecreëerd", h.Name); return(RedirectToAction("Index")); } catch (Exception ex) { ModelState.AddModelError("", ex.Message); } } // http policy requires to fill the list again after the post ehvm.Branch = branchRepo.FindAll().Select(t => new SelectListItem { Value = t.Name, Text = t.Name }); ehvm.ContactPerson = contactRepo.FindAll().Select(t => new SelectListItem { Value = t.ContactPersonId.ToString(), Text = t.LastName + " " + t.FirstName }); ehvm.Owner = ownerRepo.FindAll().Select(t => new SelectListItem { Value = t.OwnerId.ToString(), Text = t.LastName + " " + t.FirstName }); ehvm.Status = statusRepo.FindAll().Select(t => new SelectListItem { Value = t.St, Text = t.St }); return(View("Create", ehvm)); }