コード例 #1
0
        public ActionResult Create(SubCatCreate model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.CategoryId = BuildCategoryDropdown(model.CategoryId);
                return(View(model));
            }

            var service = CreateSubCatService();

            if (service.CreateSubCat(model))
            {
                TempData["SaveResult"] = $"'{model.SubCatName}' was created";
                return(RedirectToAction("Index"));
            }
            ;

            ModelState.AddModelError("", $"'{model.SubCatName}' could not be created.");

            ViewBag.CategoryId = BuildCategoryDropdown(model.CategoryId);
            return(View(model));
        }
コード例 #2
0
        public bool CreateSubCat(SubCatCreate model)
        {
            var entity =
                new SubCategory()
            {
                CategoryId       = model.CategoryId,
                SubCatName       = model.SubCatName,
                SubCatMaxAllowed = model.SubCatMaxAllowed,
                CreateBy         = _userId,
                CreateAt         = DateTimeOffset.Now
            };

            using (var ctx = new ApplicationDbContext())
            {
                ctx.SubCategories.Add(entity);

                bool success = true;
                try { ctx.SaveChanges(); }
                catch { success = false; }

                return(success);
            }
        }