public IActionResult Edit(PostEditViewModel model, IFormFile file) { var post = context.Posts.FirstOrDefault(i => i.Id == model.Id); if (ModelState.IsValid) { if (file != null) { if (System.IO.File.Exists($"wwwroot\\img\\{model.ExistImage}")) { System.IO.File.Delete($"wwwroot\\img\\{model.ExistImage}"); } var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\img", file.FileName); using (var fs = new FileStream(path, FileMode.Create)) { file.CopyTo(fs); model.ImageUrl = file.FileName; } } post.PostTitle = model.PostTitle; post.PostName = model.PostName; post.PostDescription = model.Description; post.ImageUrl = model.ImageUrl; post.PostDate = DateTime.Now; context.SaveChanges(); return(RedirectToAction("Index", "Home")); } return(View(model)); //Sabah Dersde Yazacam!!! }
public IActionResult Remove(int?id) { var c = context.Comments.Include(i => i.User).FirstOrDefault(i => i.Id == id); if (User.Identity.Name == c.User.UserName) { int Id = Convert.ToInt32(TempData["postId"]); var comment = context.Comments.FirstOrDefault(i => i.Id == id); context.Comments.Remove(comment); context.SaveChanges(); return(RedirectToAction("Details", new { Id = PostID })); } return(View("Error")); }