// GET: ArticleManage/Create
 public ActionResult Create()
 {
     var vm = new ArticleManageCreateViewModel
     {
         PostTime=DateTime.Now,
         ArticleCategories=db.ArticleCategories.ToList(),
         ArticleDialogs =new List<ArticleDialog> { new ArticleDialog() },
         Characters=db.Characters.ToList()
     };
     return View(vm);
 }
        public ActionResult Create(ArticleManageCreateViewModel model)
        {
            if (ModelState.IsValid)
            {
                for (int i = 0; i < model.ArticleDialogs.Count; i++)
                {
                    model.ArticleDialogs[i].SequenceNumber = i + 1;
                }
                var article = new Article
                {
                    Title = model.Title,
                    CoverImgUrl = model.CoverImgUrl,
                    ShortDescription = model.ShortDescription,
                    Description = model.Description,
                    Content = model.Content,
                    Meta = model.Meta,
                    UrlSlug = model.UrlSlug,
                    IsPublished = model.IsPublished,
                    PostTime = model.PostTime,
                    ModifyTime = model.ModifyTime,

                    CategoryId=db.ArticleCategories.Find(model.ArticleCategoryForArticle).Id,
                    Category= db.ArticleCategories.Find(model.ArticleCategoryForArticle),

                    Dialogs= model.ArticleDialogs
                };
                db.Articles.Add(article);
                db.SaveChanges();

                if (model.CoverImg != null)
                {
                    var uploadedFile = new UploadedFile(model.CoverImg);
                    var coverImgName = uploadedFile.SaveAsWithGuid(Server.MapPath("~/ImgRepository/ArticleImgs/" + article.Id));

                    var pathRel = Url.Content("~/ImgRepository/ArticleImgs/" + article.Id + "/" + coverImgName);
                    article.CoverImgUrl = pathRel;

                    db.Entry(article).State = EntityState.Modified;
                    db.SaveChanges();
                }

                var tagsList = ArticleHandler.ConvertTagsStringToList(model.ArticleTagsForArticle);
                //check tags and save
                foreach (var tagName in tagsList)
                {
                    if (!db.ArticleTags.Any(t => t.Name == tagName))
                    {
                        var articleTag = new ArticleTag
                        {
                            Name = tagName
                        };
                        db.ArticleTags.Add(articleTag);
                        db.SaveChanges();
                    }
                }

                article.Tags = db.ArticleTags.Where(t => tagsList.Contains(t.Name)).ToList();
                db.Entry(article).State = EntityState.Modified;
                db.SaveChanges();

                return RedirectToAction("Index");
            }

            return View(model);
        }