public ActionResult Create([FromBody] CategoryCreateViewModel collection) { try { if (ModelState.IsValid) { Category newCategory = new Category() { Name = collection.CategoryName, }; foreach (var item in collection.AttributesID) { newCategory.CategoryAttributes.Add(new CategoryAttributes() { CategoryAttributeId = item }); } category.Add(newCategory); category.SaveAll(); return(Ok("Category has been Added")); } return(BadRequest("Categoty Data is not valid")); } catch { return(BadRequest("Categoty Data is not valid")); } }
public ActionResult Create(Category category) { if (ModelState.IsValid) { categoryService.Add(category); return(RedirectToAction("Index")); } return(View(category)); }
public async Task <ActionResult> Add(CategoryDtos categoryDtos) { try { var category = _mapper.Map <Category>(categoryDtos); var data = await _categoryRepositry.Add(category); if (data == null) { return(BadRequest("This category could not be added.")); } return(Ok($"the {data.Name} category has been created.")); } catch (Exception ex) { return(BadRequest(ex.Message)); } }