public async Task <IActionResult> Edit(int id, [Bind("Id,Title,Description,ShortDescription,Image,UrlRus,CategoryId")] SubcategoriesModel subcategoriesModel, IFormFile imageFile) { if (id != subcategoriesModel.Id) { return(NotFound()); } if (imageFile != null || subcategoriesModel.Image == null) { subcategoriesModel.Image = ImageHelper.AddImage(_appEnvironment, imageFile); } if (ModelState.IsValid) { try { _context.Update(subcategoriesModel); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SubcategoriesModelExists(subcategoriesModel.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Title", subcategoriesModel.CategoryId); return(View(subcategoriesModel)); }
public async Task <IActionResult> Create([Bind("Id,Title,Description,ShortDescription,Image,UrlRus,CategoryId")] SubcategoriesModel subcategoriesModel, IFormFile imageFile) { if (imageFile != null) { subcategoriesModel.Image = ImageHelper.AddImage(_appEnvironment, imageFile); } if (ModelState.IsValid) { _context.Add(subcategoriesModel); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["CategoryId"] = new SelectList(_context.Categories, "Id", "Title", subcategoriesModel.CategoryId); return(View(subcategoriesModel)); }
public void CreateSubcategories(SubcategoriesModel model) { List <Subcategory> subcategories = new List <Subcategory>(); for (int i = 0; i < model.Subcategories.Count; i++) { if (_database.CategoryRepository.Get(model.Subcategories[i].ParentCategoryId) == null) { throw new Exception("Category is NULL"); } Category parentCategory = _database.CategoryRepository.Get(model.Subcategories[i].ParentCategoryId); Subcategory subcategory = _mapper.Map <Subcategory>(model.Subcategories[i]); subcategory.ParentCategory = parentCategory; subcategories.Add(subcategory); } _database.SubcategoryRepository.AddRange(subcategories); _database.Save(); }
public IActionResult AddSubcategories([FromBody] SubcategoriesModel subcategories) { _categoryService.CreateSubcategories(subcategories); return(Ok()); }