Esempio n. 1
0
 public ActionResult Create(EditPost model)
 {
     if (ModelState.IsValid)
     {
         var post = GetPostFromModel(model);
         //Action ac = () => _blogRepository.AddPost(model.Post);
         //getvalidationerrors(ac);
         _blogRepository.AddPost(post);
         SetDbMessage(true, string.Format("New post created: {0}", model.Post.Title));
         return RedirectToAction("Index");
     }
     InitModel(model);
     ShowErrors();
     return View(model);
 }
Esempio n. 2
0
        private Post GetPostFromModel(EditPost model)
        {
            var dbPost = _blogRepository.Post(model.Post.Id);

            dbPost.Title = model.Post.Title;
            dbPost.CategoryId = model.SelectedCategory;
            dbPost.ShortDescription = model.Post.ShortDescription;
            dbPost.Description = model.Post.Description;
            dbPost.UrlSlug = model.Post.UrlSlug;
            dbPost.Meta = model.Post.Meta;
            dbPost.LastModified = DateTime.UtcNow;
            dbPost.Published = model.Post.Published;

            if (model.SelectedTags != null && model.SelectedTags.Length > 0)
            {
                dbPost.Tags.Clear();
                foreach (var tag in model.SelectedTags)
                {
                    dbPost.Tags.Add(_blogRepository.Tag(tag));
                }

            }

            return dbPost;
        }
Esempio n. 3
0
 //
 // GET: /BlogAdmin/Post/Create
 public ActionResult Create()
 {
     var model = new EditPost() { Post = new Post(){Tags = new Collection<Tag>()}};
     InitModel(model);
     return View(model);
 }
Esempio n. 4
0
 private void InitModel(EditPost editPost)
 {
     editPost.Categories = _blogRepository.Categories().ToList();
     editPost.Tags = _blogRepository.Tags().ToList();
 }
Esempio n. 5
0
        private Post GetPostFromModel(EditPost model)
        {
            Post result = null;
            //create
            if (model.Post.Id == 0)
            {
                model.Post.CategoryId = model.SelectedCategory;
                model.Post.PostedOn = DateTime.UtcNow;
                result = model.Post;
            }
            else //edit
            {
                result = _blogRepository.Post(model.Post.Id);
                result.Title = model.Post.Title;
                result.CategoryId = model.SelectedCategory;
                result.ShortDescription = model.Post.ShortDescription;
                result.Description = model.Post.Description;
                result.UrlSlug = model.Post.UrlSlug;
                result.Meta = model.Post.Meta;
                result.LastModified = DateTime.UtcNow;
                result.Published = model.Post.Published;
                result.Tags.Clear();

            }
            //update/insert tags
            if (model.SelectedTags != null && model.SelectedTags.Any())
            {
                result.Tags = _blogRepository.Tags().Where(x => model.SelectedTags.Contains(x.Id)).ToList();
            }

            return result;
        }
Esempio n. 6
0
        public ActionResult Edit(EditPost model)
        {
            if (ModelState.IsValid)
            {
                var post = GetPostFromModel(model);
                _blogRepository.EditPost(post);
                //getvalidationerrors(_blogRepository.Context);
                SetDbMessage(true, string.Format("Category edited: {0}", model.Post.Title));

                return RedirectToAction("Index");
            }
            InitModel(model);
            ShowErrors();
            return View(model);
        }
Esempio n. 7
0
        //
        // GET: /BlogAdmin/Post/Edit/5
        public ActionResult Edit(int id)
        {
            var post  = _blogRepository.Post(id);
            if (post == null)
            {
                return HttpNotFound();
            }
            var model = new EditPost()
                    {
                        Post = post,
                        SelectedCategory = post.Category.Id,
                        SelectedTags = post.Tags.Select(x => x.Id).ToArray()
                    };

            InitModel(model);
            return View(model);
        }