// GET: Admin/SubCategories/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } //var subCategory = await _db.SubCategory.FindAsync(id); var subCategory = await _db.SubCategory.SingleOrDefaultAsync(m => m.Id == id); if (subCategory == null) { return(NotFound()); } var MySubCategoryViewModel = new SubCategoryCreateViewModel(); var CategoryLists = await _db.Category.ToListAsync(); MySubCategoryViewModel.CategoryLists = new SelectList(CategoryLists, "Id", "Name"); MySubCategoryViewModel.SubCategory = subCategory; MySubCategoryViewModel.SubCategoryList = await _db.SubCategory .OrderBy(p => p.Name) .Select(p => p.Name) .Distinct() .ToListAsync(); return(View(MySubCategoryViewModel)); }
public async Task <IActionResult> Edit(SubCategoryCreateViewModel model) { if (ModelState.IsValid) { if (SubCategoryExists(model.SubCategory.Name, model.SubCategory.CategoryId)) { StatusMessage = $"Error: {model.SubCategory.Name} already exists under this category. Please enter another Sub-Category."; } else { try { _dbContext.Update(model.SubCategory); await _dbContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } catch (DbUpdateConcurrencyException) { throw; } } } model.Categories = _dbContext.Categories.OrderBy(c => c.Name).ToList(); model.ExistingSubCategories = _dbContext.SubCategories.OrderBy(s => s.Name).Select(s => s.Name).Distinct().ToList(); model.StatusMessage = StatusMessage; return(View(model)); }
// GET: Admin/SubCategory/Create public IActionResult Create() { var model = new SubCategoryCreateViewModel() { Categories = _dbContext.Categories.OrderBy(c => c.Name).ToList(), SubCategory = new Models.SubCategory(), ExistingSubCategories = _dbContext.SubCategories.OrderBy(s => s.Name).Select(s => s.Name).Distinct().ToList() }; return(View(model)); }
public IActionResult Create(int Id) { //Get Category name Category cat = _categoryDataService.GetSingle(p => p.CategoryId == Id); SubCategoryCreateViewModel vm = new SubCategoryCreateViewModel { CategoryName = cat.Name, CategoryId = Id }; return(View(vm)); }
// GET: Admin/SubCategories/Create public async Task <IActionResult> Create() { var MySubCategoryViewModel = new SubCategoryCreateViewModel(); var CategoryLists = await _db.Category.ToListAsync(); MySubCategoryViewModel.CategoryLists = new SelectList(CategoryLists, "Id", "Name"); MySubCategoryViewModel.SubCategory = new Models.SubCategory(); MySubCategoryViewModel.SubCategoryList = await _db.SubCategory .OrderBy(p => p.Name) .Select(p => p.Name) .Distinct() .ToListAsync(); return(View(MySubCategoryViewModel)); }
// GET: Admin/SubCategory/Edit/5 public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } var subCategory = await _dbContext.SubCategories.FindAsync(id); var model = new SubCategoryCreateViewModel() { Categories = _dbContext.Categories.OrderBy(c => c.Name).ToList(), SubCategory = subCategory, ExistingSubCategories = _dbContext.SubCategories.OrderBy(s => s.Name).Select(s => s.Name).Distinct().ToList() }; return(View(model)); }
public IActionResult Create(SubCategoryCreateViewModel vm) { if (ModelState.IsValid) { // map the vm SubCategory s = new SubCategory { SubCategoryName = vm.SubCategoryName, CategoryId = vm.CategoryId }; // save to db _subCcategoryDataService.Create(s); // Go gome ModelState.Clear(); return(RedirectToAction("Index", "SubCategory", new { Id = vm.CategoryId })); } // if not valid return(View(vm)); }
public async Task <IActionResult> Edit(int id, SubCategoryCreateViewModel SubCategoryModelView) { if (ModelState.IsValid) { var DoesSubCategoriesExists = _db.SubCategory .Include(s => s.Category) .Where(s => s.Name == SubCategoryModelView.SubCategory.Name && s.Category.Id == SubCategoryModelView.SubCategory.CategoryId); if (DoesSubCategoriesExists.Count() > 0) { // Error StatusMessage = $"Error: {SubCategoryModelView.SubCategory.Name} " + $"Exists Under {DoesSubCategoriesExists.First().Category.Name}"; } else { var GetSubCategoryFrom_Db = await _db.SubCategory.FindAsync(id); GetSubCategoryFrom_Db.Name = SubCategoryModelView.SubCategory.Name; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } // Again Retrive everything from Database like Index method.... var MySubCategoryViewModel = new SubCategoryCreateViewModel(); var CategoryLists = await _db.Category.ToListAsync(); MySubCategoryViewModel.CategoryLists = new SelectList(CategoryLists, "Id", "Name"); MySubCategoryViewModel.SubCategory = SubCategoryModelView.SubCategory; MySubCategoryViewModel.SubCategoryList = await _db.SubCategory .OrderBy(p => p.Name) .Select(p => p.Name) .Distinct() .ToListAsync(); MySubCategoryViewModel.StatusMessage = StatusMessage; return(View(MySubCategoryViewModel)); }