public async Task <IActionResult> Create(ProjectViewModel projectViewModel) { if (!ModelState.IsValid) { return(View(projectViewModel)); } string fileName = null; try { fileName = await UploadFile.Add(projectViewModel.ImageUpload); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } if (fileName == null) { return(View(projectViewModel)); } projectViewModel.MainImage = fileName; var project = _mapper.Map <Project>(projectViewModel); await _projectRep.Add(project); await _projectRep.SaveChanges(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Edit(Guid id, ProjectImageViewModel projectImageViewModel) { if (id != projectImageViewModel.Id) { return(NotFound()); } ModelState.Remove("ProjectViewModel.Name"); ModelState.Remove("ProjectViewModel.Description"); if (!ModelState.IsValid) { return(View(projectImageViewModel)); } //Delete Old File if (projectImageViewModel.Image != null) { UploadFile.Delete(projectImageViewModel.Image); } string fileName = null; try { fileName = await UploadFile.Add(projectImageViewModel.ImageUpload); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } if (fileName == null) { return(View(projectImageViewModel)); } projectImageViewModel.Image = fileName; var projectImage = _mapper.Map <ProjectImage>(projectImageViewModel); projectImage.ProjectId = projectImageViewModel.ProjectId; await _projectImageRep.Update(projectImage); await _projectImageRep.SaveChanges(); return(RedirectToAction("Index", new { id = projectImageViewModel.ProjectId })); }
public async Task <IActionResult> Edit(Guid id, ProjectViewModel projectViewModel) { if (id != projectViewModel.Id) { return(NotFound()); } // Update image if (projectViewModel.ImageUpload != null) { // Delete Old image UploadFile.Delete(projectViewModel.MainImage); string fileName = null; try { fileName = await UploadFile.Add(projectViewModel.ImageUpload); } catch (Exception ex) { ModelState.AddModelError(string.Empty, ex.Message); } if (fileName == null) { return(View(projectViewModel)); } projectViewModel.MainImage = fileName; } var project = _mapper.Map <Project>(projectViewModel); await _projectRep.Update(project); await _projectRep.SaveChanges(); return(RedirectToAction(nameof(Index))); }