コード例 #1
0
ファイル: PostService.cs プロジェクト: Eifersuuucht/WebBlog
        public void UpdatePost(UpdatePostBindModel bindModel)
        {
            var post = _context.Posts.Find(bindModel.Id);

            if (post == null)
            {
                throw new Exception("Unable to find the post");
            }
            if (post.IsDeleted)
            {
                throw new Exception("Unable to update a deleted post");
            }

            bindModel.UpdatePost(post);
            _context.SaveChanges();
        }
コード例 #2
0
        public IActionResult Edit(UpdatePostBindModel bindModel)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _service.UpdatePost(bindModel);
                    return(RedirectToAction(nameof(View), new { id = bindModel.Id }));
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError(string.Empty, "An Error occured saving the post");
            }

            return(View(bindModel));
        }