예제 #1
0
        public async Task <IActionResult> Create(SubCategoryAnd_CategoryViewModel model)
        {
            if (ModelState.IsValid)
            {
                var doesSubCategoryExist = _db.SubCategory.Include(s => s.Category).Where(s => s.Name == model.SubCategory.Name && s.Category.Id == model.SubCategory.CategoryId);

                if (doesSubCategoryExist.Count() > 0)
                {
                    //error
                    StatusMessage = "Error : Sub Category exists under" + doesSubCategoryExist.First().Category.Name + " Category. Please use another Name";
                }
                else
                {
                    _db.SubCategory.Add(model.SubCategory);
                    await _db.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            SubCategoryAnd_CategoryViewModel modelVM = new SubCategoryAnd_CategoryViewModel()
            {
                CategoryList    = await _db.Category.ToListAsync(),
                SubCategory     = model.SubCategory,
                SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(),
                StatusMessage   = StatusMessage
            };

            return(View(modelVM));
        }
예제 #2
0
        //Get - Create
        public async Task <IActionResult> Create()
        {
            SubCategoryAnd_CategoryViewModel model = new SubCategoryAnd_CategoryViewModel()
            {
                CategoryList    = await _db.Category.ToListAsync(),
                SubCategory     = new Models.SubCategory(),
                SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync(),
            };

            return(View(model));
        }
예제 #3
0
        //Get - Edit
        public async Task <IActionResult> Edit(int id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            var subCategory = await _db.SubCategory.SingleOrDefaultAsync(m => m.Id == id);

            if (subCategory == null)
            {
                return(NotFound());
            }

            SubCategoryAnd_CategoryViewModel model = new SubCategoryAnd_CategoryViewModel()
            {
                CategoryList    = await _db.Category.ToListAsync(),
                SubCategory     = subCategory,
                SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync(),
            };

            return(View(model));
        }