public async Task <IActionResult> Edit(int Id, HomePageCoversEditBindingModel model) { if (Id != model.Id) { return(NotFound()); } if (ModelState.IsValid) { var image = _context.HomePageCovers.AsNoTracking().Where(i => i.Id == Id).FirstOrDefault(); if (image == null) { return(NotFound()); } using (var stream = System.IO.File.Create(GetFilePathUpdate(model.UploadImage.FileName))) { await model.UploadImage.CopyToAsync(stream); UploadImageHelper.ResizeAndSaveImage(stream, GetThumbFilePathUpdate(model.UploadImage.FileName)); } image.Title = model.Title; image.FileName = model.UploadImage.FileName; _context.Update(image); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { return(View()); } }
public async Task <IActionResult> Edit(int id, TeacherEditBindingModel teacher) { if (id != teacher.Id) { return(NotFound()); } if (ModelState.IsValid) { try { string filePath = null; //upload image if (teacher.ImageFile != null && teacher.ImageFile.Length > 0) { filePath = await UploadImageAsync(teacher.ImageFile); if (filePath == null) { ModelState.AddModelError("ImageFile", "Invalid File Type!"); return(View());//todo } } else { filePath = teacher.ImageFileFromDatabase; } Teacher teacherToUpdate = new Teacher { Id = teacher.Id, Name = teacher.Name, Description = teacher.Description.Replace("\r\n", "<br />").Replace("\n", "<br />"), CoverPhoto = filePath }; _context.Update(teacherToUpdate); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(teacher.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(teacher)); }
public async Task <IActionResult> Edit(int id, PostBindingModel postData) { if (id != postData.Id) { return(NotFound()); } if (ModelState.IsValid) { try { string filePath = null; //upload image if (postData.ImageFile != null && postData.ImageFile.Length > 0) { filePath = await UploadImageAsync(postData); if (filePath == null) { ModelState.AddModelError("ImageFile", "Invalid File Type!"); return(View());//todo } } else if (postData.ConfirmDelete == true) { filePath = null; } else { filePath = postData.ImageFileFromDatabase; } Post oldpost = _context.Posts.AsNoTracking().Where(p => p.Id == postData.Id).FirstOrDefault(); Post PostToAdd = new Post { Id = postData.Id, Title = postData.Title, Description = postData.Description.Replace("\r\n", "<br />").Replace("\n", "<br />"), ImageFile = filePath, CreatedDate = oldpost.CreatedDate }; _context.Update(PostToAdd); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PostExists(postData.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(postData)); }
public async Task <IActionResult> Edit(int id, AlbumEditBindingModel model) { if (id != model.Id) { return(NotFound()); } if (ModelState.IsValid) { try { Album oldAlbum = _context.Albums.AsNoTracking().Where(a => a.Id == model.Id).FirstOrDefault(); string folderName = FileHelpers.GetValidAlbumFolderName(model.Title.Trim()); string albumFolder = GetAlbumPath(folderName); string albumThumbFolder = Path.Combine(albumFolder, "thumb"); //if the album name has been changed, then update folder name if (oldAlbum.Title.Trim() != model.Title.Trim()) { string sourcePath = GetAlbumPath(oldAlbum.AlbumFolderName); if (sourcePath != albumFolder) { Directory.Move(sourcePath, albumFolder); } } //replace image if changed string fileName = string.Empty; if (model.UploadImage != null) { fileName = await PrepareImage.CoverPhotoAsync(model.UploadImage, albumFolder, albumThumbFolder, "AlbumCover"); } Album newAlbum = new Album() { Id = model.Id, Title = model.Title.Trim(), CoverPhoto = string.IsNullOrEmpty(fileName) ? oldAlbum.CoverPhoto : fileName, AlbumFolderName = folderName, Description = model.Description }; _context.Update(newAlbum); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AlbumExists(model.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } return(View(model)); }