예제 #1
0
        public async Task <IActionResult> Create([Bind("Id,Slug,Title,Content,PostParent,Status,MainImageId")] Post post)
        {
            if (ModelState.IsValid)
            {
                post.Creation = DateTime.Now.ToString("yyyyMMdd");
                var rawSlug = post.Slug == null | post.Slug?.Trim().Length != 0
                            ? post.Title
                            : post.Slug;
                post.Slug = Slug.GetUniqSlug(rawSlug, _context.Posts.Select(c => c.Slug).ToList());
                _context.Add(post);
                var categoryIds = Request.Form["Categorys"];
                if (categoryIds.Count() != 0)
                {
                    foreach (var categoryId in categoryIds)
                    {
                        post.PostCategorys.Add(new PostCategory {
                            CategoryId = Int32.Parse(categoryId), PostId = post.Id
                        });
                    }
                }
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(post));
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("Id,Title,Slug")] Category category)
        {
            if (ModelState.IsValid)
            {
                var rawSlug = category.Slug == null | category.Slug?.Trim().Length != 0
                            ? category.Title
                            : category.Slug;
                category.Slug = Slug.GetUniqSlug(rawSlug, _context.Categorys.Select(c => c.Slug).ToList());
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }