Esempio n. 1
0
        public ActionResult Edit(int id)
        {
            var category = _context.Categories.SingleOrDefault <Category>(c => c.Id == id);

            if (category == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new CategoryFormViewModel();

            viewModel.Breadcrumb = CategoriesTools.GetBreadcrumb(_context, category);
            viewModel.Category   = category;

            return(View("CategoryForm", viewModel));
        }
Esempio n. 2
0
        public ActionResult Save(Category category)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CategoryFormViewModel();
                viewModel.Breadcrumb = CategoriesTools.GetBreadcrumb(_context, category);
                viewModel.Category   = category;
                return(View("CategoryForm", viewModel));
            }

            if (category.Id != 0)
            {
                var categoryInDB = _context.Categories.Single <Category>(c => c.Id == category.Id);
                categoryInDB.Name = category.Name;
            }
            else
            {
                _context.Categories.Add(category);
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Categories"));
        }