Exemple #1
0
        public async Task<IActionResult> Edit(EditPostViewModel editPost)
        {
            if (ModelState.IsValid)
            {
                var currentuser = _userManager.Users.FirstOrDefault(x => x.UserName == User.Identity.Name);
                Post post = new Post {
                    PostId = editPost.PostId,
                    Title = editPost.Title,
                    Content = editPost.Content,
                    Permalink = editPost.Permalink,
                    DateCreated = editPost.DateCreated,
                    Tags = editPost.Tags,
                    Author = currentuser,
                    Blog = _context.Blogs.First(x=>x.Id == editPost.Blog),
                    PostStatus = _context.PostStatuses.First(x=>x.Id == editPost.PostStatus)
                };

                _context.Update(post);
                await _context.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            ViewBag.PostStatus = new SelectList(_context.PostStatuses, "Id", "Status", editPost.PostStatus);
            ViewBag.BlogId = new SelectList(_context.Blogs, "Id", "Name", editPost.Blog);
            return View(editPost);
        }
Exemple #2
0
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return HttpNotFound();
            }

            Post post = await _context.Posts.Include(a => a.Author).Include(p => p.PostStatus).Include(p => p.Blog).SingleAsync(m => m.PostId == id);
            if (post == null)
            {
                return HttpNotFound();
            }

            EditPostViewModel editPost = new EditPostViewModel
            {
                Title = post.Title,
                Content = post.Content,
                Permalink = post.Permalink,
                DateCreated = post.DateCreated,
                Tags = post.Tags,
                Blog = post.Blog.Id,
                PostId = post.PostId,
                Author = post.Author.Id,
                PostStatus = post.PostStatus.Id
            };

            ViewBag.PostStatus = new SelectList(_context.PostStatuses, "Id", "Status", post.PostStatus.Id);
            ViewBag.BlogId = new SelectList(_context.Blogs, "Id", "Name", post.Blog.Id);

            return View(editPost);
        }