public ActionResult Create([Bind(Include = "Id,Body, BlogId")]Comment comment, BlogViewModel BlogViewModel) { var currentUser = UserManager.FindById(User.Identity.GetUserId()); string userName = ""; bool isLoggedIn = false; //getting current user if (currentUser != null) { userName = currentUser.UserName; isLoggedIn = true; if (ModelState.IsValid) { comment.Author = userName; comment.CreatedAt = DateTime.Now; comment.BlogId = BlogViewModel.Id; db.Comments.Add(comment); db.SaveChanges(); return RedirectToAction("Details", "Blogs", new { id = comment.BlogId }); } return View(BlogViewModel); } else { isLoggedIn = false; ViewBag.isLoggedIn = isLoggedIn; return RedirectToAction("Login", "Account"); } }
// GET: Comments/Delete/5 public ActionResult Delete(int? id, BlogViewModel blogViewModel) { var currentUser = UserManager.FindById(User.Identity.GetUserId()); string userName = currentUser.UserName; bool canEdit = false; if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Comment comment = db.Comments.Find(id); if (comment == null) { return HttpNotFound(); } if (currentUser != null && currentUser.Equals(comment.Author)) { canEdit = true; } ViewBag.canEdit = canEdit; return View(comment); }