public ActionResult Create(House house) { if (ModelState.IsValid) { List<Cost> costs = new List<Cost>(); foreach (Cost c in house.Costs) { if (c.Name != null) costs.Add(c); } List<Photo> photos = new List<Photo>(); foreach (Photo p in house.Photos) { if (p.URL != null) photos.Add(p); } house.Costs = costs; house.Photos = photos; db.Houses.Add(house); db.SaveChanges(); foreach (Photo p in house.Photos) { string temp = Request.PhysicalApplicationPath + "/Images/temp/"; string dest = Request.PhysicalApplicationPath + "/Images/houses/"; System.IO.File.Move(temp + p.URL, dest + p.URL); System.IO.File.Move(temp + p.URLThumb, dest + p.URLThumb); System.IO.File.Move(temp + p.URLThumbWide, dest + p.URLThumbWide); } Success(house.Name + " was successfully created!"); return RedirectToAction("Index"); } Error("Something went wrong!"); return View(house); }
public ActionResult Edit(House house) { if (ModelState.IsValid) { //House h = db.Houses.Include("Location").Include("Owner").Include("Owner.Location").Include("ManagementCompany").Include("ManagementCompany.Location").Single(m => m.ID == house.ID); /*h.Name = house.Name; h.SecurityCode = house.SecurityCode; h.PhoneNumber = house.PhoneNumber; h.Rate = house.Rate; h.Location.Address = house.Location.Address;*/ House h = db.Houses.Include("Location").Include("Owner").Include("Owner.Location").Include("ManagementCompany").Include("ManagementCompany.Location").Single(m => m.ID == house.ID); db.Entry(h).CurrentValues.SetValues(house); db.Entry(h.Location).CurrentValues.SetValues(house.Location); db.Entry(h.Owner).CurrentValues.SetValues(house.Owner); db.Entry(h.Owner.Location).CurrentValues.SetValues(house.Owner.Location); db.Entry(h.ManagementCompany).CurrentValues.SetValues(house.ManagementCompany); db.Entry(h.ManagementCompany.Location).CurrentValues.SetValues(house.ManagementCompany.Location); //db.Entry(house).State = EntityState.Modified; //db.Entry(house.Location).State = EntityState.Modified; //db.Entry(house.Owner).State = EntityState.Modified; //db.Entry(house.ManagementCompany).State = EntityState.Modified; List<Cost> newCosts = house.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.House = h; db.Entry(c).State = EntityState.Added; } //db.Costs.Add(c); } } List<Photo> newPhotos = house.Photos; for (int i = 0; i < newPhotos.Count; i++) { Photo p = newPhotos.ElementAt(i); if (p.ID > 0) { if (p.URL == null) { string path = Request.PhysicalApplicationPath + "Images\\houses\\"; p.URL = p.URLThumb.Replace(".thumb", ""); System.IO.File.Delete(path + p.URL); System.IO.File.Delete(path + p.URLThumb); System.IO.File.Delete(path + p.URLThumbWide); db.Entry(p).State = EntityState.Deleted; } else db.Entry(p).State = EntityState.Modified; } else { if (p.URL != null) { p.House = h; db.Entry(p).State = EntityState.Added; string temp = Request.PhysicalApplicationPath + "Images\\temp\\"; string dest = Request.PhysicalApplicationPath + "Images\\houses\\"; System.IO.File.Move(temp + p.URL, dest + p.URL); System.IO.File.Move(temp + p.URLThumb, dest + p.URLThumb); System.IO.File.Move(temp + p.URLThumbWide, dest + p.URLThumbWide); } } } db.SaveChanges(); Success("Changes were successfully saved!"); return RedirectToAction("Index"); } Error("Something went wrong! Changes were not saved."); return View(house); }