Пример #1
0
        protected virtual void PrepareModel(ArticleModel model, Article article = null)
        {
            if (model == null)
                throw new ArgumentNullException("model");

            //所有分类
            model.Categories.AddRange(
                _categoryService.GetAllCategory().Select(t => new SelectListItem
                {
                    Text = t.Name,
                    Value = t.Id.ToString(),
                })
            );

            //所有标签
            model.Tags = _articleService.GetAllTag().Select(t => new KeyValueModel
            {
                Text = t.Name,
                Value = t.Id.ToString()
            }).ToList();

            //选中标签
            if (article != null)
            {
                model.SelectedTags = article.Tags.Select(t => t.Id).ToList();
            }
            
        }
Пример #2
0
        public ActionResult Create(ArticleModel model, bool continueEditing)
        {
            if (ModelState.IsValid)
            {
                var article = model.ToEntity();
                article.CreateDate = DateTime.Now;
                article.UpdateDate = DateTime.Now;

                if (model.CategoryId == 0)
                    article.CategoryId = null;

                //标签
                foreach (var id in model.SelectedTags)
                {
                    article.Tags.Add(_articleService.GetTagById(id));
                }

                _articleService.InsertArticle(article);

                SuccessNotification("添加成功");

                return continueEditing ? RedirectToAction("Edit", new { id = article.Id }) : RedirectToAction("List");
            }

            PrepareModel(model);

            return View(model);
        }
Пример #3
0
        public ActionResult Edit(ArticleModel model, bool continueEditing)
        {
            var article = _articleService.GetArticleById(model.Id);

            if (ModelState.IsValid)
            {
                int prevPictureId = article.PictureId;

                article = model.ToEntity(article);
                article.UpdateDate = DateTime.Now;

                if (model.CategoryId == 0)
                    article.CategoryId = null;

                //标签
                var allTags = _articleService.GetAllTag().ToList();
                foreach (var tag in allTags)
                {
                    if (model.SelectedTags != null && model.SelectedTags.Contains(tag.Id))
                    {
                        if (article.Tags.Count(t => t.Id == tag.Id) == 0)
                        {
                            article.Tags.Add(tag);
                        }
                    }
                    else
                    {
                        if (article.Tags.Count(t => t.Id == tag.Id) > 0)
                        {
                            article.Tags.Remove(tag);
                        }
                    }
                }

                _articleService.UpdateArticle(article);

                //图片处理, 删除旧图片
                if (prevPictureId > 0 && prevPictureId != article.PictureId)
                {
                    var prevPicture = _pictureService.GetPictureById(prevPictureId);
                    if (prevPicture != null)
                        _pictureService.DeletePicture(prevPicture);
                }

                SuccessNotification("保存成功");
                return continueEditing ? RedirectToAction("Edit", new { id = article.Id }) : RedirectToAction("List");
            }

            PrepareModel(model, article);

            return View(model);
        }
Пример #4
0
        public ActionResult Create()
        {
            var model = new ArticleModel();
            PrepareModel(model);

            return View(model);
        }