コード例 #1
0
        public IActionResult Create(CategoryViewModel model)
        {
            CategoryViewModel viewModel = null;
            try
            {
                if(!ModelState.IsValid)
                    throw new Exception("The Category model is not valid!");

                _libraryContext.Categories.Add(model.Category);
                if (_libraryContext.SaveChanges() == 0)
                {
                   throw new Exception("The Category model could not be saved!");
                }

                //Success(CreateMessage(ControllerActionType.Create, "library", model.Name), true);

                return RedirectToAction("Index");
            }
            catch(Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Unable to save changes.");

                viewModel = new CategoryViewModel
                {
                    Category = model.Category,
                    Categories = _libraryContext.Categories.AsEnumerable().OrderBy(m => m.Name)
                };
            }
            return View(viewModel);
        }
コード例 #2
0
        public IActionResult Create()
        {
            var model = new CategoryViewModel
            {
                Category = new Category(),
                Categories = _libraryContext.Categories.AsEnumerable().OrderBy(m => m.Name)
            };

            return View(model);
        }
コード例 #3
0
        public IActionResult Edit(CategoryViewModel model)
        {
            try
            {
                if(!ModelState.IsValid)
                    throw new Exception("The Category model is not valid!");

                var originalModel = _libraryContext.Categories.FirstOrDefault(m => m.Id == model.Category.Id);

                if(originalModel == null)
                    throw new Exception("The existing Category: " + model.Category.Name + " doesn't exists anymore!");

                originalModel.Name = model.Category.Name;
                originalModel.Description = model.Category.Description;
                originalModel.ParentCategoryId = model.Category.ParentCategoryId;

                _libraryContext.Categories.Attach(originalModel);
                _libraryContext.Entry(originalModel).State = EntityState.Modified;

                if (_libraryContext.SaveChanges() == 0)
                {
                   throw new Exception("The Category model could not be saved!");
                }

                return RedirectToAction("Index");

            }
            catch(Exception ex)
            {
                ModelState.AddModelError(string.Empty, "Unable to save changes.");
            }
            return View(model);
        }
コード例 #4
0
        public IActionResult Edit(Int16? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(400);
            }

            var model = _libraryContext.Categories.FirstOrDefault(m => m.Id == id);

            if(model == null)
            {
                return RedirectToAction("Index");
            }

            var viewModel = new CategoryViewModel
            {
                Category = model,
                Categories = _libraryContext.Categories.AsEnumerable().Where(m => m.Id != id).OrderBy(m => m.Name)
            };

            return View(viewModel);
        }