public async Task <IActionResult> Create(TeacherCreateBindingModel teacher) { if (ModelState.IsValid) { string filePath = null; //upload image if (teacher.ImageFile != null && teacher.ImageFile.Length > 0) { filePath = await UploadImageAsync(teacher.ImageFile); if (filePath == null) { ModelState.AddModelError("InvoiceFile", "Invalid File Type!"); return(View());//todo } } Teacher teacherToAdd = new Teacher { Name = teacher.Name, Description = teacher.Description.Replace("\r\n", "<br />").Replace("\n", "<br />"), CoverPhoto = filePath }; _context.Add(teacherToAdd); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(teacher)); }
public async Task <IActionResult> Create(PostBindingModel postData) { if (ModelState.IsValid) { string filePath = null; //upload image if (postData.ImageFile != null && postData.ImageFile.Length > 0) { filePath = await UploadImageAsync(postData); if (filePath == null) { ModelState.AddModelError("InvoiceFile", "Invalid File Type!"); return(View());//todo } } Post PostToAdd = new Post { Title = postData.Title, Description = postData.Description.Replace("\r\n", "<br />").Replace("\n", "<br />"), ImageFile = filePath, CreatedDate = DateTime.Now, }; _context.Add(PostToAdd); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(postData)); }
public async Task <IActionResult> Create(AlbumCreateBindingModel album)//* { if (ModelState.IsValid) { //generate folder name, remove any chatacters that are not allowed string albumFolderName = FileHelpers.GetValidAlbumFolderName(album.Title.Trim()); //create album folder string albumPath = GetAlbumPath(albumFolderName); if (!Directory.Exists(albumPath)) { Directory.CreateDirectory(albumPath); } //create thumbs folder string albumPathThumb = Path.Combine(albumPath, "thumb"); if (!Directory.Exists(albumPathThumb)) { Directory.CreateDirectory(albumPathThumb); } string fileName; if (album.UploadImage != null) { fileName = await PrepareImage.CoverPhotoAsync(album.UploadImage, albumPath, albumPathThumb, "AlbumCover"); if (fileName == null) { ModelState.AddModelError("InvoiceFile", "Invalid File Type!"); return(View()); } } else { fileName = "AlbumCover.jpg"; System.IO.File.Copy(Path.Combine(_configuration.GetValue <string>("CustomSettings:ImagesRootPath", ".\\Images"), fileName), Path.Combine(albumPath, fileName)); } Album newAlbum = new Album() { AlbumFolderName = albumFolderName, Title = album.Title, CoverPhoto = fileName, Description = album.Description }; _context.Add(newAlbum); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(album)); }