public ActionResult Create(Post p) { try { string currentUserId = User.Identity.GetUserId(); List <User> GetUsers = sp.GetUsers(); var user = GetUsers.FirstOrDefault(u => u.Id == currentUserId); Post post = new Post { Message = p.Message, Titre = p.Titre, PostedDate = p.PostedDate, UserId = currentUserId, }; if (user != null) { user.Posts.Add(post); sp.Commit(); su.Commit(); } return(RedirectToAction("Index", "Post")); } catch (Exception ex) { return(View()); } }
public ActionResult AddComment(CommentsVM comment, int postId) { //bool result = false; Comment commentEntity = null; string currentUserId = User.Identity.GetUserId(); int userId = 1; //int.Parse(currentUserId); //(int)Session["UserID"]; List <User> GetUsers = sp.GetUsers(); var user = GetUsers.FirstOrDefault(u => u.Id == currentUserId); var post = sp.GetPosts().FirstOrDefault(p => p.PostID == postId); if (comment != null) { commentEntity = new Comment { CommentMsg = comment.CommentMsg, CommentedDate = DateTime.Now }; if (user != null && post != null) { post.Comments.Add(commentEntity); user.Comments.Add(commentEntity); //sq.SaveChanges( ); //dbContext.SaveChanges(); //result = true; sp.Commit(); su.Commit(); } } return(RedirectToAction("GetComments", "Comments", new { postId = postId })); }