public async Task <IActionResult> Details(int?id) { if (id == null) { return(NotFound()); } var auther = await db.Authers .FirstOrDefaultAsync(m => m.Id == id); var autherViewModel = new AutherViewModel() { Id = auther.Id, AutherName = auther.AutherName, Qualification = auther.Qualification, Experience = auther.Experience, WrittenDate = auther.WrittenDate, PublishDate = auther.PublishDate, Venue = auther.Venue, ExistingImage = auther.ProfilePicture }; if (auther == null) { return(NotFound()); } return(View(auther)); }
public async Task <IActionResult> Edit(int id, AutherViewModel model) { if (ModelState.IsValid) { var auther = await db.Authers.FindAsync(model.Id); auther.AutherName = model.AutherName; auther.Qualification = model.Qualification; auther.Experience = model.Experience; auther.WrittenDate = model.WrittenDate; auther.PublishDate = model.PublishDate; auther.Venue = model.Venue; if (model.AutherPicture != null) { if (model.ExistingImage != null) { string filePath = Path.Combine(webHostEnvironment.WebRootPath, "Uploads", model.ExistingImage); System.IO.File.Delete(filePath); } auther.ProfilePicture = ProcessUploadedFile(model); } db.Update(auther); await db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View()); }
private string ProcessUploadedFile(AutherViewModel model) { string uniqueFileName = null; if (model.AutherPicture != null) { string uploadsFolder = Path.Combine(webHostEnvironment.WebRootPath, "Uploads"); uniqueFileName = Guid.NewGuid().ToString() + "_" + model.AutherPicture.FileName; string filePath = Path.Combine(uploadsFolder, uniqueFileName); using (var fileStream = new FileStream(filePath, FileMode.Create)) { model.AutherPicture.CopyTo(fileStream); } } return(uniqueFileName); }
public async Task <IActionResult> Create(AutherViewModel model) { if (ModelState.IsValid) { string uniqueFileName = ProcessUploadedFile(model); Auther auther = new Auther { AutherName = model.AutherName, Qualification = model.Qualification, Experience = model.Experience, WrittenDate = model.WrittenDate, PublishDate = model.PublishDate, Venue = model.Venue, ProfilePicture = uniqueFileName }; db.Add(auther); await db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(model)); }