Пример #1
0
        public async Task <IActionResult> Create([Bind("Id,BlogCategoryId,Title,Abstract,PostBody,IsReady")] CategoryPost categoryPost, IFormFile formFile)
        {
            if (ModelState.IsValid)
            {
                categoryPost.Created = DateTime.Now;

                categoryPost.ContentType = _imageService.RecordContentType(formFile);
                categoryPost.ImageData   = await _imageService.EncodeFileAsync(formFile);

                //using slugs
                var slug = _slugService.URLFriendly(categoryPost.Title);
                if (_slugService.Isunique(_context, slug))
                {
                    categoryPost.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Title", "This Title has a duplicate slug!");
                    ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name");
                    return(View(categoryPost));
                }
                _context.Add(categoryPost);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name", categoryPost.BlogCategoryId);
            return(View(categoryPost));
        }