public ActionResult Create(SubCategoryViewModel subCategory) { if (!ModelState.IsValid) { return(View(subCategory)); } try { //Add to Database var subCategoryDto = Mapper.Map <SubCategoryDto>(subCategory); _subCategoryService.AddSubCategory(subCategoryDto); TempData["Notification"] = "Successfully!"; //Return new list in Index var list = _subCategoryService.ListSubCategory(); var viewModel = Mapper.Map <List <SubCategoryViewModel> >(list) ?? new List <SubCategoryViewModel>(); return(View("Index", viewModel)); } catch (Exception e) { return(RedirectToAction("Create")); } }
//Add SubCategory Ajax public ActionResult AddSubCategoryAjax([FromBody] SubCategoryForPost subCategoryModel) { if (!_workContext.CurrentCustomer.IsRegistered()) { return(Unauthorized()); } if (!_workContext.CurrentCustomer.IsInCustomerRole(RolesType.Administrators, true) && !_workContext.CurrentCustomer.IsInCustomerRole(RolesType.ConsultationAdmin, true)) { return(Forbid()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var subCategory = _subCategory.AddSubCategory(subCategoryModel); if (subCategory == null) { return(Json(new { result = false })); } return(Json(new { result = true })); }
public async Task <IActionResult> Create(SubCategoryAndCategoryViewModel model) { if (ModelState.IsValid) { var subcategory = await subCategoryService.DoesSubCategoryExists(model); if (subcategory != null) { //Error StatusMessage = "Error : Sub Category exists under " + subcategory.Category.Name + " category. Please use another name."; } else { await subCategoryService.AddSubCategory(model.SubCategory); return(RedirectToAction(nameof(Index))); } } var subcategoriesList = await subCategoryService.GetAllSubCategories(); var modelMV = new SubCategoryAndCategoryViewModel() { CategoryList = await categoryService.GetAllCategories(), SubCategory = new SubCategory(), SubCategoryList = subcategoriesList.OrderBy(n => n.Name).Distinct().Select(s => s.Name).ToList(), StatusMessage = StatusMessage }; return(View(modelMV)); }
public IActionResult Create(SubCategory subCategory) { try { var newSubCategory = _subCategoryService.AddSubCategory(subCategory); return(CreatedAtRoute("GetSubCategoryById", new { id = newSubCategory.ID }, newSubCategory)); } catch (Exception) { return(BadRequest("Value is null")); } }