示例#1
0
        public ActionResult ViewPostByTag(string tag)
        {
            if (tag == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }

            var postTag = _db.Tags.FirstOrDefault(p => p.Title == tag);
            if (postTag == null)
            {
                // If the tag does not exist, display an empty list of posts
                postTag = new Tag { Title = tag, BlogPosts = new List<BlogPost>() };
            }

            postTag.BlogPosts = postTag.BlogPosts.ToOrderedList().ToList();

            return View("ViewPostByTag", postTag);
        }
示例#2
0
        private void SyncPostTags(BlogPost post, string tagsString)
        {
            var tagStrings = ParseTags(tagsString);

            if (post.Tags == null)
            {
                post.Tags = new List<Tag>();
            }

            var newPostTags = new List<Tag>();
            foreach (var tagString in tagStrings)
            {
                var tag = _db.Tags.FirstOrDefault(t => t.Title.Equals(tagString, StringComparison.CurrentCultureIgnoreCase));
                if (tag == null)
                {
                    tag = new Tag { Title = tagString };
                    _db.Tags.Add(tag);
                }
                newPostTags.Add(tag);

                if (!post.Tags.Any(t => t.Title == tag.Title))
                {
                    post.Tags.Add(tag);
                }
            }

            foreach (var tag in post.Tags.ToArray())
            {
                if (!newPostTags.Any(t => t.Title == tag.Title))
                {
                    post.Tags.Remove(tag);
                }
            }
        }