public virtual ActionResult Edit(AboutSectionEditVM vm) { if (!ModelState.IsValid) { return Json(new { success = false }); } try { var aboutSection = unitOfWork.AboutSectionRepository.GetByID(vm.AboutSectionId); if (aboutSection == null) aboutSection = unitOfWork.AboutSectionRepository.Insert(new AboutSection() { AboutSectionId = vm.AboutSectionId }); AboutSectionMapper.GetAboutSection(vm, ref aboutSection); // Image save HttpPostedFileBase file = Request.Files["uploadPhoto"]; if (file != null && file.ContentLength > 0) { string extension = Path.GetExtension(file.FileName); string aboutSectionPath = Server.MapPath(Constants.AboutSectionImagesDir); string path = Path.Combine( aboutSectionPath, aboutSection.AboutSectionId.ToString() + extension); //Check if file is image if (file.ContentType.StartsWith("image/")) { //Delete old image var oldPath = Path.Combine( aboutSectionPath, aboutSection.AboutSectionId.ToString() + aboutSection.FileExtension); if (System.IO.File.Exists(oldPath)) { System.IO.File.Delete(oldPath); } aboutSection.FileExtension = extension; file.SaveAs(path); } } unitOfWork.Save(); return Json(new { success = true }); } catch { return Json(new { success = false }); } }
public static void GetAboutSection(AboutSectionEditVM vm, ref AboutSection aboutSection) { if (aboutSection == null) { aboutSection = new AboutSection(); } if (vm != null) { aboutSection.HeaderBG = vm.HeaderBG; aboutSection.HeaderEN = vm.HeaderEN; aboutSection.TextBG = vm.TextBG; aboutSection.TextEN = vm.TextEN; aboutSection.OrderNum = vm.OrderNum; } }
public static AboutSectionEditVM GetVM(AboutSection aboutSection) { var vm = new AboutSectionEditVM(); if (aboutSection == null) aboutSection = new AboutSection() { AboutSectionId = Guid.NewGuid() }; vm.AboutSectionId = aboutSection.AboutSectionId; vm.FileName = aboutSection.FileName; vm.HeaderBG = aboutSection.HeaderBG; vm.HeaderEN = aboutSection.HeaderEN; vm.TextBG = aboutSection.TextBG; vm.TextEN = aboutSection.TextEN; vm.OrderNum = aboutSection.OrderNum; return vm; }