public ActionResult Create(Booking booking) { ViewData["ClientsList"] = Client.BuildClientsDropdownList(); ViewData["HousesList"] = House.BuildHousesDropdownList(); if (ModelState.IsValid && House.IsAvailableInRange(booking.StartDate, booking.EndDate, booking.House.ID)) { booking.House = db.Houses.Include("Location").Include("Owner").Include("Owner.Location").Include("ManagementCompany").Include("ManagementCompany.Location").Single(m => m.ID == booking.House.ID); List<Cost> costs = new List<Cost>(); foreach (Cost c in booking.Costs) { if (c.Name != null) costs.Add(c); } booking.Costs = costs; if (booking.Client.ID != 0) { Client client = booking.Client; db.Entry(client).State = EntityState.Modified; booking.Client = db.Clients.Include("Location").Single(m => m.ID == client.ID); } db.Bookings.Add(booking); db.SaveChanges(); Success("The booking was created successfully!"); return RedirectToAction("Index"); } Error("Something went wrong!"); return View(booking); }
// // PARTIAL: /Bookings/ClientEditor/5 public PartialViewResult _ClientEditor(int id = 0) { ViewBag.NewClient = true; if (id == 0) return PartialView(); Booking booking = new Booking(); booking.Client = db.Clients.Include("Location").Single(m => m.ID == id); ViewBag.NewClient = false; return PartialView(booking); }
public ActionResult Edit(Booking booking) { if (ModelState.IsValid) { //House temp = db.Houses.Include("Location").Single(m => m.ID == booking.House.ID); //db.Entry(temp).CurrentValues.SetValues(booking.House); //db.Entry(temp).CurrentValues.SetValues(booking.House);//db.Entry(booking.House).State = EntityState.Modified; //db.Entry(booking).CurrentValues.SetValues(booking); //db.Entry(booking).State = EntityState.Modified; //db.Entry(booking.Client).State = EntityState.Modified; //db.Entry(booking.Client.Location).State = EntityState.Modified; Booking b = db.Bookings.Include("House").Include("House.Location").Include("House.Owner").Include("House.Owner.Location").Include("House.ManagementCompany").Include("House.ManagementCompany.Location").Include("Client").Include("Client.Location").Single(m => m.ID == booking.ID); db.Entry(b).CurrentValues.SetValues(booking); db.Entry(b.Client).CurrentValues.SetValues(booking.Client); db.Entry(b.Client.Location).CurrentValues.SetValues(booking.Client.Location); List<Cost> newCosts = booking.Costs; for (int i = 0; i < newCosts.Count; i++) { Cost c = newCosts.ElementAt(i); if (c.ID > 0) { if (c.Name == null) { db.Entry(c).State = EntityState.Deleted; //i--; } else db.Entry(c).State = EntityState.Modified; } else { if (c.Name != null) { //c.Booking = new Booking(); //c.Booking.ID = booking.ID; c.Booking = b; db.Entry(c).State = EntityState.Added; } } } /*for (int i = 0; i < booking.Costs.Count; i++) { //Cost c = newCosts.ElementAt(i); if (booking.Costs[i].ID > 0) { if (booking.Costs[i].Name == null) //db.Costs.Remove(c); db.Entry(booking.Costs[i]).State = EntityState.Deleted; else db.Entry(booking.Costs[i]).State = EntityState.Modified; } else { if (booking.Costs[i].Name != null) { //booking.Costs[i].Booking = booking; db.Entry(booking.Costs[i]).State = EntityState.Added; } } }*/ db.SaveChanges(); Success("Changes were successfully saved!"); return RedirectToAction("Index"); } Error("Something went wrong! Changes were not saved."); return View(booking); }