public ActionResult UpdateAchivement(HomeUIAchivementViewModels achivement) { try { using (DbContextTransaction transaction = _context.Database.BeginTransaction()) { StringBuilder container = new StringBuilder(); StringBuilder elements = new StringBuilder(); _ImageCropper backgroundImg = achivement.Background.FirstOrDefault(); SaveImage(backgroundImg); container.Append(RenderPartialViewToString("~/Views/Home/_Achivement_Section_Container.cshtml", backgroundImg)); achivement.Achivements.ToList().ForEach(x => elements.Append(RenderPartialViewToString("~/Views/Home/_Achivement_Section_Container_Item.cshtml", x))); container.Replace("{{elements}}", elements.ToString()); UICache achivementHtml = _context.UICaches.Find(Page.Home, PageComponent.Home_Achivement, DataType.Html); if (achivementHtml == null) { _context.UICaches.Add( achivementHtml = new UICache(Page.Home, PageComponent.Home_Achivement, DataType.Html)); }//create if html type is not existed achivementHtml.DataCache = container.ToString(); UICache achivementJson = _context.UICaches.Find(Page.Home, PageComponent.Home_Achivement, DataType.Json); if (achivementJson == null) { _context.UICaches.Add( achivementJson = new UICache(Page.Home, PageComponent.Home_Achivement, DataType.Json)); }//create if json type is not existed achivementJson.DataCache = JsonConvert.SerializeObject( new { achivement.Achivements, Background = achivement.Background.Select(x => new _ImageCropper { ImagePath = x.ImagePath, ImageName = x.ImageName, IsUploaded = true, }).ToList() }); _context.SaveChanges(); transaction.Commit(); }//end of transaction return(RedirectToAction(nameof(UpdateAchivement))); } catch (Exception) { return(new HttpStatusCodeResult(HttpStatusCode.InternalServerError)); } }
public ActionResult Update(int?id, UpdatePropertyViewModels property) { if (id.HasValue && ModelState.IsValid) { Property baseProperty = _context.Properties.Include(x => x.PropertyImages) .FirstOrDefault(x => x.Id == id.Value); if (baseProperty != null) { using (DbContextTransaction transaction = _context.Database.BeginTransaction()) { _converter.UpdateProperty(property, baseProperty); _ImageCropper avatar = SaveImage(property.Avatar.FirstOrDefault()); baseProperty.AvatarName = avatar.ImageName; baseProperty.AvatarPath = avatar.ImagePath; List <PropertyImage> baseImages = baseProperty.PropertyImages.ToList(); baseImages.ForEach(x => _context.Entry(x).State = EntityState.Deleted); SaveImages(property.PropertyImages).ToList().ForEach(x => { if (x.IsUploaded) { PropertyImage baseImage = baseImages.FirstOrDefault(y => y.Path == x.ImagePath); _context.Entry(baseImage).State = EntityState.Unchanged; } else { baseProperty.PropertyImages.Add(new PropertyImage { Name = x.ImageName, Path = x.ImagePath, }); }//end if handle case image is modified or not });//end for upload and write image to physical file _context.SaveChanges(); transaction.Commit(); }//end transaction return(RedirectToAction(nameof(Index))); } //end if handle case model is not valid } //end if handle case args is null return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); }
protected _ImageCropper SaveImage(_ImageCropper img) { string _defaultImage = Url.DefaultImage(); string _imageStorage = Url.ImageStorage(); if (img == null) { img = new _ImageCropper(_defaultImage, _imageStorage + _defaultImage); } else { if (!img.IsUploaded) { string finalName = (img.GetType() == typeof(_ImageCropper) || !string.IsNullOrEmpty(img.ImageData)) ? Server.MapPath(_imageStorage).WriteBase64ToFile(img.ImageName, img.ImageData.Split(',')[1]) : _defaultImage; img.ImageName = finalName; img.ImagePath = _imageStorage + finalName; } //end if handle case image is uploaded already } //end if handle case args is null return(img); }
public ActionResult SubmitProperty(CreatePropertyViewModels property) { ViewBag.PropertyTypes = GetPropertyTypes(property.BasicInformation.PropertyTypeId); ViewBag.PropertyStatusCodes = GetPropertyStatusCodes(property.BasicInformation.PropertyTypeId); ViewBag.Cities = GetCities(property.BasicInformation.City); ViewBag.Districts = GetDistricts(property.BasicInformation.City, property.BasicInformation.District); if (ModelState.IsValid) { using (DbContextTransaction transaction = _context.Database.BeginTransaction()) { Property newProperty = _converter.CreateProperty(property); _ImageCropper avatar = SaveImage(property.Avatar.FirstOrDefault()); newProperty.AvatarName = avatar.ImageName; newProperty.AvatarPath = avatar.ImagePath; SaveImages(property.PropertyImages).ToList().ForEach(x => { newProperty.PropertyImages.Add(new PropertyImage { Name = x.ImageName, Path = x.ImagePath }); }); _context.Entry(newProperty).State = EntityState.Added; _context.SaveChanges(); transaction.Commit(); }//end transaction return(RedirectToAction(nameof(Index))); }//end if handle case model is not valid return(View(property)); }