Ejemplo n.º 1
0
        public virtual ActionResult Edit(EditViewModel viewModel, string slug = null)
        {
            // Ensure valid
            if (!ModelState.IsValid)
            {
                // Not valid - Go back to the edit page
                return Edit(slug);
            }

            // If slug is not specified, we're creating a new post
            var post = string.IsNullOrEmpty(slug)
                ? new PostModel()
                : _blogRepository.GetBySlug(slug);

            // Valid, so save the post using a whitelist of fields allowed to be updated from the UI
            UpdateModel(post, "Post", new[] { "Title", "Slug", "Date", "Published", "RawContent", "MainCategoryId" });
            _blogRepository.Save(post);

            // Now save categories and tags
            // Make sure main category is always included in categories
            var categories = (viewModel.PostCategoryIds ?? Enumerable.Empty<int>()).Union(new[] { viewModel.Post.MainCategoryId });

            _blogRepository.SetCategories(post, categories);
            _blogRepository.SetTags(post, viewModel.PostTagIds ?? Enumerable.Empty<int>());

            // Clear any cache for this post
            _webCache.ClearCache(post);

            TempData["topMessage"] = string.Format(
                "{0}: Saved changes to {1}. <a href=\"{2}\" target=\"_blank\">View post</a>.", DateTime.Now.ToLongTimeString(),
                Server.HtmlEncode(post.Title), Url.BlogPost(post));
            return Redirect(Url.BlogPostEdit(post));
        }
Ejemplo n.º 2
0
        public virtual ActionResult Edit(EditViewModel viewModel, string slug = null)
        {
            // Ensure valid
            if (!ModelState.IsValid)
            {
                // Not valid - Go back to the edit page
                return Edit(slug);
            }

            // If slug is not specified, we're creating a new post
            var post = string.IsNullOrEmpty(slug)
                ? new PostModel()
                : _blogRepository.GetBySlug(slug);

            // Valid, so save the post using a whitelist of fields allowed to be updated from the UI
            // Currently broken due to https://github.com/aspnet/Mvc/issues/2799, so for now we'll just
            // manually update all the things.
            //await TryUpdateModelAsync(post, "Post", x => x.Title, x => x.Slug, x => x.Date, x => x.Published, x => x.RawContent, x => x.MainCategoryId, x => x.Summary);
            post.Title = viewModel.Post.Title;
            post.Slug = viewModel.Post.Slug;
            post.Date = viewModel.Post.Date;
            post.Published = viewModel.Post.Published;
            post.RawContent = viewModel.Post.RawContent;
            post.MainCategoryId = viewModel.Post.MainCategoryId;
            post.Summary = viewModel.Post.Summary;

            _blogRepository.Save(post);

            // Now save categories and tags
            // Make sure main category is always included in categories
            var categories = (viewModel.PostCategoryIds ?? Enumerable.Empty<int>()).Union(new[] { viewModel.Post.MainCategoryId });

            _blogRepository.SetCategories(post, categories);
            _blogRepository.SetTags(post, viewModel.PostTagIds ?? Enumerable.Empty<int>());

            TempData["topMessage"] = string.Format(
                "{0}: Saved changes to {1}. <a href=\"{2}\" target=\"_blank\">View post</a>.",
                DateTime.Now.ToLongTimeString(),
                HtmlEncoder.Default.HtmlEncode(post.Title), Url.BlogPost(post)
            );
            return Redirect(Url.BlogPostEdit(post));
        }