public IActionResult Save(CommentViewModel commentModel) { try { if (!ModelState.IsValid) { throw new Exception("Invalid ModelState"); } var commentToSave = new Comment(); if (commentModel.ParentCommentId != 0) { commentToSave = _repo.Comment(commentModel.ParentCommentId); var childComment = Mapper.Map <Comment>(commentModel); childComment.CreatedDate = DateTime.Now; childComment.Owner = _userManager.FindByNameAsync(commentModel.User).Result; commentToSave.Replies.Add(childComment); } else if (commentModel.CommentId != 0) { commentToSave = _repo.Comment(commentModel.CommentId); commentToSave.Content = commentModel.Content; commentToSave.ModifiedDate = DateTime.Now; } else { Mapper.Map(commentModel, commentToSave); commentToSave.ParentPost = _repo.Post(commentModel.Year, commentModel.Month, commentModel.PostTitle); commentToSave.CreatedDate = DateTime.Now; commentToSave.Owner = _userManager.FindByNameAsync(commentModel.User).Result; } _repo.AddOrUpdateComment(commentToSave); Response.StatusCode = (int)HttpStatusCode.Created; return(Json(new { Message = "Comment saved" })); } catch (Exception ex) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return(Json(new { Message = ex.Message })); } }