public string Delete(string Id) { Hotel_Package hotelPackage = db.Hotel_Package.FirstOrDefault(g => g.Id == Id); List <Hotel_Reservation> hotelReservation = db.Hotel_Reservation.Where(r => r.ID_Package == Id).ToList(); if (hotelReservation.Count != 0) { return("已有预约"); } if (hotelPackage != null) { var hotelPackageImages = db.Hotel_Package_Image.Where(h => h.HotelPackage_Id == hotelPackage.Id); foreach (var hotelPackageImage in hotelPackageImages) { db.Hotel_Package_Image.Remove(hotelPackageImage); } db.Hotel_Package.Remove(hotelPackage); if (db.SaveChanges() > 0) { return("删除成功"); } } return("删除失败"); }
public string Add(FormCollection formCollection) { string photoUrl = "", area = "", address = "", hotelId = "", country = "", phone = "", name = "", description = ""; StringBuilder allPhotoUrls = new StringBuilder(); foreach (var key in formCollection.AllKeys) { area = formCollection["area"]; description = formCollection["description"]; address = formCollection["address"]; hotelId = formCollection["hotelId"]; country = formCollection["country"]; phone = formCollection["phone"]; name = formCollection["name"]; } if (Request.Files.Count > 0) { try { // Get all files from Request object HttpFileCollectionBase files = Request.Files; for (int i = 0; i < files.Count; i++) { //string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/"; //string filename = Path.GetFileName(Request.Files[i].FileName); HttpPostedFileBase file = files[i]; // Checking for Internet Explorer if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER") { string[] testfiles = file.FileName.Split(new char[] { '\\' }); photoUrl = testfiles[testfiles.Length - 1]; } else { photoUrl = file.FileName; } if (!System.IO.Directory.Exists(Server.MapPath("~/Uploads/"))) { System.IO.Directory.CreateDirectory(Server.MapPath("~/Uploads/")); } // Get the complete folder path and store the file inside it. string fullPathUrl = Path.Combine(Server.MapPath("~/Uploads/"), photoUrl); file.SaveAs(fullPathUrl); allPhotoUrls.Append(photoUrl + ";"); } // Returns message that successfully uploaded //return "File Uploaded Successfully!"; } catch (DbEntityValidationException dbEx) { StringBuilder sb = new StringBuilder(); foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { sb.Append("Property:" + validationError.PropertyName + " Error: " + validationError.ErrorMessage); } } return(sb.ToString()); } } else { return("No files selected."); } string[] photos = allPhotoUrls.ToString().Split(';'); Hotel_Package hotelPackage = new Hotel_Package() { Id = Guid.NewGuid().ToString(), Description = description, Area = area, Country = country, Photo = "/Uploads/" + photos[0], SingRommPhoto = "/Uploads/" + photos[1], DoubleRoomPhoto = "/Uploads/" + photos[2], OtherRoomPhoto = "/Uploads/" + photos[3], Hotel = db.Hotels.Where(h => h.Id == hotelId).FirstOrDefault(), ID_Hotel = hotelId }; db.Hotel_Package.Add(hotelPackage); if (db.SaveChanges() > 0) { return("添加成功"); } return("添加失败"); }