public ActionResult Create()
        {
            var subCatModel = new CreateSubCategoryBindingModel
            {
                Categories = this.populator.GetCategories()
            };

            return this.View(subCatModel);
        }
        public ActionResult Create(CreateSubCategoryBindingModel model)
        {
            if(model != null && this.ModelState.IsValid)
            {
                var subCategory = new SubCategory
                {
                    Title = model.Title,
                    CategoryId = model.CategoryId
                };
                this.Data.SubCategories.Add(subCategory);
                this.Data.SaveChanges();
                return this.RedirectToAction("Index", "Home");
            }

            model.Categories = this.populator.GetCategories();
            return this.View(model);
        }
示例#3
0
        public async Task <bool> CreateSubCategory(CreateSubCategoryBindingModel model)
        {
            var subCategory = DbContext.SubCategories.FirstOrDefault(sc => sc.Name == model.Name);

            if (subCategory != null)
            {
                return(false);
            }

            subCategory = new SubCategory()
            {
                Name = model.Name
            };

            var category = DbContext.Categories.Find(model.CategoryId);

            category.SubCategories.Add(subCategory);
            await DbContext.SaveChangesAsync();

            return(true);
        }