public ActionResult DeleteConfirmed(int id) { Property property = model.Properties.FirstOrDefault(x => x.ID == id); var inOrder_Installment = model.Installment_Contract.Where(x => x.Property_ID == id); var inOrder_FullContact = model.Full_Contract.Where(x => x.Property_ID == id); var orn = model.Property_Service.Where(x => x.Property_ID == property.ID).ToArray(); if (inOrder_FullContact.Count() != 0 || inOrder_Installment.Count() != 0) { TempData["msg"] = "<script>alert('BẤT ĐỘNG SẢN HIỆN ĐANG TRONG QUÁ TRÌNH CÓ HỢP ĐỒNG!!! VUI LÒNG QUAY LẠI.');</script>"; } else { for (int i = 0; i < orn.Length; i++) { var tmp = orn[i]; Property_Service psDell = model.Property_Service.FirstOrDefault(x => x.Property_ID == tmp.Property_ID); model.Property_Service.Remove(psDell); model.SaveChanges(); } model.Properties.Remove(property); model.SaveChanges(); PopularMessage(true); return(RedirectToAction("Index")); } PopularMessage(false); return(View(property)); }
public ActionResult Create([Bind(Include = "Property_Name,Property_Type_ID,Description,District_ID,Address" + ",Area,Bed_Room,Bath_Room,Price,Installment_Rate,Avatar,Album,Property_Status_ID")] Property property, HttpPostedFileBase[] files, HttpPostedFileBase file , List <int> Service_ID) { if (ModelState.IsValid) { using (var scope = new TransactionScope()) { model.Properties.Add(property); string album = ""; Random random = new Random(); if (files != null) { foreach (var imageFile in files) { if (imageFile != null) { var fileName = random.Next(1, 99999).ToString() + Path.GetFileName(imageFile.FileName); var physicalPath = Path.Combine(Server.MapPath("~/Images/"), fileName); // The files are not actually saved in this demo imageFile.SaveAs(physicalPath); album += album.Length > 0 ? ";" + fileName : fileName; } } } property.Album = album; if (file != null) { var avatar = random.Next(1, 99999).ToString() + Path.GetFileName(file.FileName); var physicPath = Path.Combine(Server.MapPath("~/Images/"), avatar); file.SaveAs(physicPath); property.Avatar = avatar; } foreach (var item in Service_ID) { Property_Service properS = new Property_Service(); properS.Property_ID = property.ID; properS.Service_ID = item; model.Property_Service.Add(properS); } model.SaveChanges(); scope.Complete(); PopularMessage(true); return(RedirectToAction("Index")); } } otherData(); PopuparData(); PopularMessage(false); return(View("Create", property)); }
public ActionResult Create(Property property, List <HttpPostedFileBase> files, List <int> Service_ID, HttpPostedFileBase file) { if (ModelState.IsValid) { using (var scope = new TransactionScope()) { model.Properties.Add(property); string album = ""; //up album if (files != null) { foreach (var imageFile in files) { if (imageFile != null) { var fileName = DateTime.Now.Ticks + "-" + Path.GetFileName(imageFile.FileName); var physicalPath = Path.Combine(Server.MapPath("~/Images"), fileName); // The files are not actually saved in this demo imageFile.SaveAs(physicalPath); album += album.Length > 0 ? ";" + fileName : fileName; } } } property.Album = album; // Avatar if (file != null) { var avatar = DateTime.Now.Ticks + "-" + Path.GetFileName(file.FileName); var physicPath = Path.Combine(Server.MapPath("~/Images"), avatar); file.SaveAs(physicPath); property.Avatar = avatar; } foreach (var item in Service_ID) { Property_Service properS = new Property_Service(); properS.Property_ID = property.ID; properS.Service_ID = item; model.Property_Service.Add(properS); } property.Installment_Rate = 0.7; model.SaveChanges(); scope.Complete(); PopularMessage(true); return(RedirectToAction("Index")); } } ViewBag.Service_ID = new MultiSelectList(model.Services.ToList(), "ID", "Service_Name", null); PopularData(); return(View()); }
public ActionResult Edit(Property property, string Property_Name, int Property_Type_ID, string Description, string Address, int Area, int Bed_Room, int Bath_Room, decimal Price, double Installment_Rate, int District_ID, int Property_Status_ID, HttpPostedFileBase[] files, HttpPostedFileBase file , List <int> Service_ID) { if (ModelState.IsValid) { using (var scrope = new TransactionScope()) { property = model.Properties.FirstOrDefault(x => x.ID == property.ID); string album = property.Album; Random random = new Random(); if (files != null) { foreach (var imageFile in files) { if (imageFile != null) { var fileName = random.Next(1, 99999).ToString() + Path.GetFileName(imageFile.FileName); var physicalPath = Path.Combine(Server.MapPath("~/Images"), fileName); // The files are not actually saved in this demo imageFile.SaveAs(physicalPath); album += album.Length > 0 ? ";" + fileName : fileName; } } } property.Album = album; // Avatar if (file != null) { var avatar = random.Next(1, 99999).ToString() + Path.GetFileName(file.FileName); var physicPath = Path.Combine(Server.MapPath("~/Images"), avatar); file.SaveAs(physicPath); property.Avatar = avatar; } // add model to database property.Address = Address; property.Area = Area; property.Description = Description; property.Bath_Room = Bath_Room; property.Bed_Room = Bed_Room; property.Installment_Rate = Installment_Rate; property.Price = Price; property.Property_Type_ID = Property_Type_ID; property.Property_Name = Property_Name; property.District_ID = District_ID; property.Property_Status_ID = Property_Status_ID; if (Service_ID != null) { var orn = model.Property_Service.Where(x => x.Property_ID == property.ID).ToArray(); for (int i = 0; i < orn.Length; i++) { var tmp = orn[i]; Property_Service psDell = model.Property_Service.FirstOrDefault(x => x.Property_ID == tmp.Property_ID); model.Property_Service.Remove(psDell); model.SaveChanges(); } } foreach (var item in Service_ID) { Property_Service properS = new Property_Service(); properS.Property_ID = property.ID; properS.Service_ID = item; model.Property_Service.Add(properS); model.SaveChanges(); } model.Entry(property).State = EntityState.Modified; model.SaveChanges(); // save file to app_data scrope.Complete(); // all done successfully return(RedirectToAction("Index")); } } otherData(); PopuparData(); return(View("Edit", property)); }