예제 #1
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Content")] PostViewModel postView)
        {
            if (id != postView.Id)
            {
                return(NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(View(postView));
            }

            try {
                var post = await _context.Posts.Where(p => id.Equals(p.Id)).SingleOrDefaultAsync();

                if (post == null)
                {
                    return(NotFound());
                }

                if (_userManager.GetUserId(User) != post.UserId)
                {
                    return(Forbid());
                }

                postView.CopyDataToModel(post);
                post.Updated = DateTime.Now;

                _context.Update(post);
                await _context.SaveChangesAsync();
            } catch (DbUpdateConcurrencyException) {
                if (!PostExists(postView.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
예제 #2
0
        public async Task <IActionResult> Create(int?ParentId, [Bind("Id,Title,Content")] PostViewModel postView)
        {
            ViewBag.ParentId = ParentId;

            if (!ModelState.IsValid)
            {
                return(View(postView));
            }

            var post = new Post();

            postView.CopyDataToModel(post);
            post.Created  = post.Updated = DateTime.Now;
            post.UserId   = _userManager.GetUserId(User);
            post.ParentId = ParentId;

            _context.Add(post);
            await _context.SaveChangesAsync();

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