Esempio n. 1
0
        public ActionResult AddOrEditCategory(EditCategoryViewModel categoryViewModel)
        {
            categoryViewModel.Uniqueness = _categoryProvider.IsUnique(categoryViewModel.CategoryId, categoryViewModel.Name);
            if (categoryViewModel.Uniqueness == UniqueValidation.Dublicate)
            {
                ModelState.AddModelError("Name", "This category is already exists");
            }
            if (categoryViewModel.Uniqueness == UniqueValidation.Error)
            {
                ModelState.AddModelError("Name", "Name is required");
            }

            Category category = ParseCategory(categoryViewModel);

            if (ModelState.IsValid)
            {
                if (categoryViewModel.CategoryId == 0)
                {
                    _categoryProvider.InsertCategory(category);
                }
                else
                {
                    _categoryProvider.UpdateCategory(category);
                }

                return(RedirectToAction("ShowCategories"));
            }
            else
            {
                categoryViewModel = ParseCategory(category);
                return(View("AddOrEditCategory", categoryViewModel));
            }
        }