//Update forum public IActionResult Update(int Id) { var forum = _forumService.GetById(Id); if (forum == null) { return(NotFound()); } var model = new ForumUpdateModel { Id = forum.Id, Title = forum.Title, Description = forum.Description, ImageForum = forum.ImageUrl }; return(View(model)); }
public async Task <IActionResult> Update(ForumUpdateModel model) { var forum = _forumService.GetById(model.Id); string urlImage = forum.ImageUrl; if (ModelState.IsValid) { string roorPath = _hostingEnviroment.WebRootPath; var files = HttpContext.Request.Form.Files; var supportedTypes = new[] { ".jpg", ".jpeg", ".png", ".gif" }; var idNew = Guid.NewGuid(); if (files.Count != 0) { var uploads = Path.Combine(roorPath, SD.FolderDefault + "/" + SD.FolderImages + "/" + SD.pathForum); var extension = Path.GetExtension(files[0].FileName).ToLower(); using (var filestream = new FileStream(Path.Combine(uploads, idNew + extension), FileMode.Create)) { files[0].CopyTo(filestream); } if (!supportedTypes.Contains(extension)) { TempData["Message"] = "Định dạng file không hợp lệ !"; return(RedirectToAction("Update", "Forum", new{ Id = model.Id })); } else { urlImage = idNew + extension; } } await _forumService.UpdateForum(model.Id, model.Title, model.Description, urlImage); return(RedirectToAction(nameof(Index))); } return(View(nameof(Update))); }