public ActionResult EditPost(AdminEditPostViewModel post, int id) { var databasePost = this.posts.GetById(id); databasePost.PostType = post.PostType; databasePost.Title = post.Title; databasePost.Content = post.Content; var pet = this.pets.GetById(post.PetId); pet.PetType = post.Pet.PetType; pet.Name = post.Pet.Name; pet.Age = post.Pet.Age; pet.Description = post.Pet.Description; pet.Color = post.Pet.Color; var location = this.locations.GetById(post.LocationId); location.City = post.Location.City; location.Street = post.Location.Street; location.AdditionalInfo = post.Location.AdditionalInfo; if (post.UploadedImage != null) { using (var memory = new MemoryStream()) { post.UploadedImage.InputStream.CopyTo(memory); var content = memory.GetBuffer(); var image = new Photo { Content = content, FileExtension = post.UploadedImage.FileName.Split(new[] { '.' }).Last() }; this.images.Update(); databasePost.Gallery.Add(image); } } this.pets.Update(); this.locations.Update(); this.posts.Update(); this.TempData["Notification"] = "Post Edited Succesfully!"; return(this.RedirectToAction("All")); }
public ActionResult Edit(int id) { var post = this.adminPosts .GetPostById(id) .FirstOrDefault(); var editPostViewModel = new AdminEditPostViewModel { Title = post.Title, Content = post.Content, CategoryId = post.CategoryId, Status = post.Status, UrlVideo = post.UrlVideo }; ViewBag.SelectedItem = populator.GetSelectedCategories(); return(View(editPostViewModel)); }
public ActionResult Edit(int id, AdminEditPostViewModel viewModel) { if (viewModel != null && ModelState.IsValid) { var post = this.adminPosts .GetPostById(id) .FirstOrDefault(); this.adminPosts.Edit(post, viewModel); this.TempData["Message"] = string.Format(GlobalConstants.SuccessMessage, " Edited Post."); return(this.RedirectToAction("Index", "AdminPosts", new { area = "Admin" })); } this.TempData["Message"] = string.Format(GlobalConstants.FailMessage, " Edited Post."); ViewBag.SelectedItem = populator.GetSelectedCategories(); return(this.View(viewModel)); }
public async Task <IActionResult> EditPost( int id, AdminEditPostViewModel model) { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account")); } if (ModelState.IsValid) { var oldPost = await _postService.GetPostByIdAsync(id, allowUnpublished : true); if (oldPost == null) { return(NotFound()); } // Tags // model.Post.Tags = model?.Tags // ?.Split(',') // ?.Select(tag => tag.Trim()) // ?.Where(tag => !string.IsNullOrEmpty(tag)) // ?.ToArray(); // Publishing model.Post.Published = oldPost.Published; if (model.IsPublished) { if (!oldPost.Published.HasValue) { model.Post.Published = DateTime.UtcNow; } } await _postService.UpdatePostAsync(model.Post); _logger.LogInformation("Person updated post with id {0}", id); } return(RedirectToAction(nameof(EditPost), new { id })); }