コード例 #1
0
        public ActionResult Edit(EditPostViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var itm = _postService.GetPost(model.Id) ?? new BlogPost {
                Tags = new List <Tag>()
            };
            var user = _userService.GetUser(User.Identity.GetUserId());

            itm.Author = user;
            itm.Title  = model.Title;
            itm.Text   = model.Text;

            itm.Tags.Clear();
            foreach (var tag in JsonConvert.DeserializeObject <string[]>(model.Tags))
            {
                if (tag.Trim().Length > 0)
                {
                    itm.Tags.Add(_tagsService.GetOrAdd(tag.Trim()));
                }
            }

            _postService.CreateOrUpdatePost(itm);
            _unitOfWork.Save();

            return(RedirectToAction("Index"));
        }