public virtual ActionResult Edit(int id = -1)
        {
            var model = new EditViewModel();

            using (var context = new WebsiteContext())
            {
                if (!context.BlogPosts.Any(x => x.BlogPostId == id))
                {
                    RedirectToAction("Manage", "Blog");
                }

                // eagerly load the tags/comments etc as the context will be disposed
                var posts = (from t in context.BlogPosts.Include("Tags")
                             where t.BlogPostId == id
                             select t);

                if (posts.Count() > 0)
                {
                    var post = posts.First();

                    model.BlogPostId  = post.BlogPostId;
                    model.BlogTitle   = post.BlogTitle;
                    model.BlogContent = post.BlogContent;
                    ViewBag.Tags      = TagHelper.GetTagArray(post.Tags);
                }
                else
                {
                    return(RedirectToAction("Manage", "Blog"));
                }
            }

            return(View(model));
        }
        public virtual ActionResult Edit(EditViewModel model, FormCollection formCollection)
        {
            var tagcsv = "";

            if (!string.IsNullOrEmpty(formCollection["tags"]))
            {
                tagcsv = formCollection["tags"];
            }
            tagcsv = tagcsv.ToLowerInvariant(); // keep your case down bro
            var tags = TagHelper.GetTagArray(tagcsv);

            if (ModelState.IsValid)
            {
                using (var context = new WebsiteContext())
                {
                    if (!context.BlogPosts.Any(x => x.BlogPostId == model.BlogPostId))
                    {
                        return(RedirectToAction("Manage", "Blog"));
                    }

                    // make sure tags exist in database, if not, they're added
                    TagHelper.AddTagRange(tags);

                    var taglist = new List <Tag>();
                    foreach (string tag in tags)
                    {
                        var tagObject = TagHelper.GetTag(context, tag);
                        taglist.Add(tagObject);
                    }

                    var post = context.BlogPosts.First(x => x.BlogPostId == model.BlogPostId);
                    post.BlogTitle    = WebHelper.StripTags(model.BlogTitle);
                    post.BlogContent  = model.BlogContent; // WebHelper.StripTags() removed. Trust your admins.
                    post.Slug         = post.BlogTitle.Slugify();
                    post.DateModified = DateTime.Now;
                    post.Tags.Clear();
                    post.Tags.AddRange(taglist);
                    context.SaveChanges();
                }

                return(RedirectToAction("Manage", "Blog"));
            }

            ViewBag.Tags = tags;
            return(View(model));
        }
        public virtual ActionResult New(NewViewModel model, FormCollection formCollection)
        {
            if (ModelState.IsValid)
            {
                var tagcsv = "";
                if (!string.IsNullOrEmpty(formCollection["tags"]))
                {
                    tagcsv = formCollection["tags"];
                }
                tagcsv = tagcsv.ToLowerInvariant(); // keep your case down bro
                var tags = TagHelper.GetTagArray(tagcsv);

                using (var context = new WebsiteContext())
                {
                    // make sure tags exist in database, if not, they're added
                    TagHelper.AddTagRange(tags);

                    var taglist = new List <Tag>();
                    foreach (string tag in tags)
                    {
                        var tagObject = TagHelper.GetTag(context, tag);
                        taglist.Add(tagObject);
                    }

                    var post = context.BlogPosts.Create();
                    post.BlogTitle   = WebHelper.StripTags(model.BlogTitle);
                    post.BlogContent = model.BlogContent; // WebHelper.StripTags(); we should trust ourselves.
                    post.Slug        = post.BlogTitle.Slugify();
                    post.DatePosted  = DateTime.Now;
                    post.Tags.AddRange(taglist);
                    context.BlogPosts.Add(post);
                    context.SaveChanges();
                }

                return(RedirectToAction("Manage", "Blog"));
            }

            return(View(model));
        }