Ejemplo n.º 1
0
        public ActionResult AddPost(BlogPost post)
        {
            //TODO: there is a bug here - even blog post could not be save to repository and exception is thrown
            //it is only model state is updated, but post instance is pass as view model. View checks model for null
            //and if it is not null, treat as successful operation. So, in case of failure it will show success message
            //together with error message.
            try
            {
                if (ModelState.IsValid)
                {
                    post.Url         = CreatePostUrl(post.Title);
                    post.CreatedDate = DateTime.Now;

                    _blogRepository.Save(post);
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError("", "Post could not be added. " + e.Message);
            }

            return(View(post));
        }