コード例 #1
0
        public ActionResult Add(CategoryAddViewModel category)
        {
            if (!ModelState.IsValid)
            {
                category.Categories = this.categories.GetAll().Select(x => new SelectListItem()
                {
                    Text = x.Name,
                    Value = x.ParentCategoryId.ToString()
                });
                return this.View(category);
            }

            var categoryToAdd = new Category()
            {
                Name = category.Name,
                ParentCategoryId = category.ParentCategoryId == null ? null : (int?)int.Parse(category.ParentCategoryId)
            };

            using (var unitOfWork = this.unitOfWork())
            {
                this.categories.Add(categoryToAdd);

                unitOfWork.Commit();
            }

            return this.RedirectToAction("Details/" + categoryToAdd.Id, "Categories");
        }
コード例 #2
0
        public ActionResult Create([DataSourceRequest] DataSourceRequest request, CategoryViewModel category)
        {
            if (category != null && ModelState.IsValid)
            {
                Category cat = new Category
                {
                    Name = category.CategoryName
                };

                db.Categories.Add(cat);
                db.SaveChanges();
            }

            return Json(new[] { category }.ToDataSourceResult(request, ModelState));
        }