public bool Update(PROPERTy entitys) { try { var property = db.PROPERTies.Find(entitys.ID); property.PropertyName = entitys.PropertyName; // save images if (entitys.ImageFile == null) { //entitys.Images = "ImagesNull.png"; } else { property.Images = entitys.Images; } // save avatar if (entitys.ImageFile2 == null) { //entitys.Avatar = "AvatarNull.png"; } else { property.Avatar = entitys.Avatar; } //property.Avatar = entitys.Avatar; //property.Images = entitys.Images; property.PropertyType_ID = entitys.PropertyType_ID; property.Content = entitys.Content; property.Street_ID = entitys.Street_ID; property.Ward_ID = entitys.Ward_ID; property.District_ID = entitys.District_ID; property.Price = entitys.Price; property.UnitPrice = entitys.UnitPrice; property.Area = entitys.Area; property.BedRoom = entitys.BedRoom; property.BathRoom = entitys.BathRoom; property.PackingPlace = entitys.PackingPlace; property.UserID = entitys.UserID; //property.Created_at = DateTime.Parse(DateTime.Now.ToString("yyyy-mm-dd")); //property.Create_post = DateTime.Parse(DateTime.Now.ToString("yyyy-mm-dd")); property.Status_ID = entitys.Status_ID; property.Note = entitys.Note; //property.Updated_at = DateTime.Parse(DateTime.Now.ToString("yyyy-mm-dd")); property.Updated_at = DateTime.Parse(DateTime.Now.ToShortDateString()); property.Sale_ID = entitys.Sale_ID; db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public long InsertProperty(PROPERTy entity) { if (entity.ImageFile == null) { entity.Images = "ImagesNull.png"; } if (entity.ImageFile2 == null) { entity.Avatar = "AvatarNull.png"; } if (entity.District_ID == null) { entity.District_ID = null; } if (entity.Status_ID == null) { entity.District_ID = null; } db.PROPERTies.Add(entity); db.SaveChanges(); return(entity.ID); }
public ActionResult Create(PROPERTy property, List <HttpPostedFileBase> files) { ListAll(); try { // Xu ly Avatar string filename2 = Path.GetFileNameWithoutExtension(property.ImageFile2.FileName); string extension2 = Path.GetExtension(property.ImageFile2.FileName); filename2 = filename2 + DateTime.Now.ToString("yymmssfff") + extension2; property.Avatar = filename2; filename2 = Path.Combine(Server.MapPath("~/Avatar"), filename2); property.ImageFile2.SaveAs(filename2); // Images string filename = Path.GetFileNameWithoutExtension(property.ImageFile.FileName); string extension = Path.GetExtension(property.ImageFile.FileName); filename = filename + DateTime.Now.ToString("yymmssfff") + extension; property.Images = filename; filename = Path.Combine(Server.MapPath("~/Images"), filename); property.ImageFile.SaveAs(filename); property.Created_at = DateTime.Parse(DateTime.Now.ToShortDateString()); if (ModelState.IsValid) { var model = new AccountModel(); long id = model.InsertProperty(property); // SavemultiImage ---------------------------- var path = ""; foreach (var item in files) { if (item != null) { if (item.ContentLength > 0) { if (Path.GetExtension(item.FileName).ToLower() == ".jpg" || Path.GetExtension(item.FileName).ToLower() == ".png" || Path.GetExtension(item.FileName).ToLower() == ".gif" || Path.GetExtension(item.FileName).ToLower() == ".jpeg") { var path0 = id + item.FileName; path = Path.Combine(Server.MapPath("~/MultiImages"), path0); item.SaveAs(path); ViewBag.UploadSuccess = true; } } } } // End SaveMultiImage ------------------------- if (id > 0) { return(RedirectToAction("Index", "Property")); } else { ModelState.AddModelError("", "Create Property khong thanh cong"); } } } catch (NullReferenceException) { property.Created_at = DateTime.Parse(DateTime.Now.ToShortDateString()); ListAll(); if (ModelState.IsValid) { var model = new AccountModel(); long id = model.InsertProperty(property); if (id > 0) { return(RedirectToAction("Index", "Property")); } else { ModelState.AddModelError("", "Create Property khong thanh cong"); } } } return(View()); }
public ActionResult Edit(PROPERTy property, List <HttpPostedFileBase> files) { ListAll(); // Images try { ViewBag.Images = Directory.EnumerateFiles(Server.MapPath("~/MultiImages")) .Select(fn => "~/MultiImages/" + Path.GetFileName(fn)); foreach (var image in (IEnumerable <string>)ViewBag.Images) { if (image.Contains(property.ID.ToString())) { System.IO.File.Delete(image); } } // Xu ly MultiImage var path = ""; foreach (var item in files) { if (item != null) { if (item.ContentLength > 0) { if (Path.GetExtension(item.FileName).ToLower() == ".jpg" || Path.GetExtension(item.FileName).ToLower() == ".png" || Path.GetExtension(item.FileName).ToLower() == ".gif" || Path.GetExtension(item.FileName).ToLower() == ".jpeg") { var path0 = property.ID + item.FileName; path = Path.Combine(Server.MapPath("~/MultiImages"), path0); item.SaveAs(path); ViewBag.UploadSuccess = true; } } } } //// Xu ly Avatar string filename2 = Path.GetFileNameWithoutExtension(property.ImageFile2.FileName); string extension2 = Path.GetExtension(property.ImageFile2.FileName); filename2 = filename2 + DateTime.Now.ToString("yymmssfff") + extension2; property.Avatar = filename2; filename2 = Path.Combine(Server.MapPath("~/Avatar"), filename2); property.ImageFile2.SaveAs(filename2); // Xu ly Images string filename = Path.GetFileNameWithoutExtension(property.ImageFile.FileName); string extension = Path.GetExtension(property.ImageFile.FileName); filename = filename + DateTime.Now.ToString("yymmssfff") + extension; property.Images = filename; filename = Path.Combine(Server.MapPath("~/Images"), filename); property.ImageFile.SaveAs(filename); // End Xu ly Images // TODO: Add insert logic here if (ModelState.IsValid) { var model = new AccountModel(); var res = model.Update(property); if (res) { return(RedirectToAction("Index", "Property")); } else { ModelState.AddModelError("", "Update không thành công"); } } } catch { if (ModelState.IsValid) { var model = new AccountModel(); var res = model.Update(property); if (res) { return(RedirectToAction("Index", "Property")); } else { ModelState.AddModelError("", "Update không thành công"); } } } return(View("Index")); // return View("Index"); }