public async Task <IActionResult> Create([Bind("Id,Name,Description,Content,Price,ImageData,ContentType,IsProductReady,RateValue,CategoryId,ItemSaleOffId,ItemStatusId,Slug")] Item item, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                item.ContentType = _imageService.RecordContentType(image);
                item.ImageData   = await _imageService.EncodeFileAsync(image);

                item.RateValue = 5;

                var slug = _slugService.URLFriendly(item.Name);
                if (_slugService.IsUnique(_context, slug))
                {
                    item.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Name", "This title cannot be used as it results in a duplicate Slug!");
                    ViewData["CategoryId"] = new SelectList(_context.Category, "Id", "Name", item.CategoryId);
                    return(View(item));
                }
                _context.Add(item);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CategoryId"]    = new SelectList(_context.Category, "Id", "Name", item.CategoryId);
            ViewData["ItemSaleOffId"] = new SelectList(_context.ItemSaleOff, "Id", "Name", item.ItemSaleOffId);
            ViewData["ItemStatusId"]  = new SelectList(_context.ItemStatus, "Id", "Name", item.ItemStatusId);
            return(View(item));
        }
        public async Task <IActionResult> Create([Bind("Id,Title,Abstract,Content,CreateDate,UpdateDate,IsproductionReady,Slug,BlogCategoryId,ImageData")] PostCategory postCategory, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                postCategory.CreateDate = DateTime.Now;
                postCategory.UpdateDate = postCategory.CreateDate;
                postCategory.ViewCount  = 0;

                postCategory.ContentType = _imageService.RecordContentType(image);
                postCategory.ImageData   = await _imageService.EncodeFileAsync(image);


                var slug = _slugService.URLFriendly(postCategory.Title);
                if (_slugService.IsUnique(_context, slug))
                {
                    postCategory.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Title", "This title cannot be used as it results in a duplicate Slug!");
                    ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name", postCategory.BlogCategoryId);
                    return(View(postCategory));
                }
                _context.Add(postCategory);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name", postCategory.BlogCategoryId);
            return(View(postCategory));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Description")] Category category)
        {
            if (ModelState.IsValid)
            {
                var slug = _slugService.URLFriendly(category.Name);
                if (_slugService.IsUnique(_context, slug))
                {
                    category.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Name", "This title cannot be used as it results in a duplicate Slug!");
                    return(View(category));
                }
                _context.Add(category);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(category));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Create([Bind("Id,BlogCategoryId,Title,Abstract,PostBody,IsProductionReady")] CategoryPost categoryPost,
                                                 IFormFile formFile)
        {
            if (ModelState.IsValid)
            {
                categoryPost.Created = DateTime.Now;

                //I can only work with the incoming image if it is not null


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


                var slug = _slugService.URLFriendly(categoryPost.Title);
                if (_slugService.IsUnique(_context, slug))
                {
                    categoryPost.Slug = slug;
                }
                else
                {
                    ModelState.AddModelError("Title", "This Title cannot be used as it results in a duplicate Slug!");
                    ViewData["BlogCategoryId"] = new SelectList(_context.BlogCategory, "Id", "Name", categoryPost.BlogCategoryId);
                    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));
        }