Beispiel #1
0
        public async Task <IActionResult> Create([Bind("Id,Name,Text,Date")] Post post)
        {
            if (ModelState.IsValid)
            {
                post.Date     = DateTime.Now;
                post.Comments = new Collection <Comment>();
                var username = User.Identity.Name;
                var sender   = _context.Users.First(x => x.UserName == username);
                post.Sender     = sender;
                post.SenderName = sender.UserName;
                _context.Add(post);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(post));
        }
        public async Task <IActionResult> Create([Bind("Name,Text,Date")] Comment comment, int?id)
        {
            comment.Id = 0;
            if (ModelState.IsValid)
            {
                comment.Date = DateTime.Now;
                var post     = _context.Posts.First(x => x.Id == id);
                var username = User.Identity.Name;
                var sender   = _context.Users.First(x => x.UserName == username);
                comment.Post       = post;
                comment.Sender     = sender;
                comment.SenderName = sender.UserName;
                _context.Add(comment);
                _sender.Send();
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(comment));
        }