public async Task <IActionResult> Create(SubCategoryAndCategoryViewModel viewModel) { if (ModelState.IsValid) { var doesCategoryExist = _db.SubCategories.Include(s => s.Category).Where(s => s.SubCategoryName == viewModel.SubCategory.SubCategoryName && s.CategoryId == viewModel.SubCategory.CategoryId); if (doesCategoryExist.Count() > 0) { StatusMessage = "Error : This sub category under " + doesCategoryExist.First().Category.CategoryName + " exist. Please use another name."; } else { _db.SubCategories.Add(viewModel.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel { CategoryList = await _db.Categories.ToListAsync(), SubCategory = new SubCategory(), SubCategoryNameList = await _db.SubCategories.OrderBy(p => p.SubCategoryName).Select(p => p.SubCategoryName).Distinct().ToListAsync(), StatusMessage = StatusMessage }; return(View(model)); }
public async Task <IActionResult> Edit(SubCategoryAndCategoryViewModel model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategories.Include(s => s.Category).Where(s => s.Name == model.SubCategory.Name && s.Category.Id == model.SubCategory.CategoryId); if (doesSubCategoryExists.Count() > 0) { //Error StatusMessage = "خطا:زیر گروه برای فهرست " + doesSubCategoryExists.First().Category.Name + " موجود می باشد لطفا نام دیگری انتخاب نمایید"; } else { var subCatFromDb = await _db.SubCategories.FindAsync(model.SubCategory.Id); subCatFromDb.Name = model.SubCategory.Name; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewModel modelVM = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Categories.ToListAsync(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategories.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), StatusMessage = StatusMessage }; //modelVM.SubCategory.Id = id; return(View(modelVM)); }
// GET EDIT // (int? id) recived id of the SubCategory that needs to be edited public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } // (m => m.Id == id) based on that id it will find the SubCategory what we want Edit // and add to the variable: var subCategory var subCategory = await _db.SubCategory.SingleOrDefaultAsync(m => m.Id == id); if (subCategory == null) { return(NotFound()); } SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = subCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }
public async Task <IActionResult> Create(SubCategoryAndCategoryViewModel model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategory.Include(s => s.Category).Where(s => s.Name == model.SubCategory.Name && s.Category.Id == model.SubCategory.CategoryId); if (doesSubCategoryExists.Count() > 0) { //Error StatusMesssage = "Error : Sub Category exists under " + doesSubCategoryExists.First().Category.Name + " category. Please use another name."; } else { _db.SubCategory.Add(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewModel modelVm = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), StatusMessage = StatusMesssage }; return(View(modelVm)); }
public async Task <IActionResult> Edit(SubCategoryAndCategoryViewModel model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategory.Include(s => s.Category).Where(s => s.Name == model.SubCategory.Name && s.Category.Id == model.SubCategory.CategoryId); if (doesSubCategoryExists.Count() > 0) { StatusMessage = "Error : Sub Category exists under " + doesSubCategoryExists.First().Category.Name + " category. Please use another name."; } else { var subCatFromDb = await _db.SubCategory.FindAsync(model.SubCategory.Id); //instead of using the Update method, which updates ALL of the properties in the mode, //we can udpate only specific properties like below subCatFromDb.Name = model.SubCategory.Name; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewModel modelVM = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), StatusMessage = StatusMessage }; modelVM.SubCategory.Id = model.SubCategory.Id; return(View(modelVM)); }
//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()); } SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = subCategory, SubCategoryList = await _db.SubCategory .OrderBy(p => p.Name) .Select(p => p.Name) .Distinct() .ToListAsync() }; return(View(model)); }
public async Task <IActionResult> Create(SubCategoryAndCategoryViewModel model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategory.Include(s => s.Category).Where(s => s.Name == model.SubCategory.Name && s.Category.Id == model.SubCategory.CategoryId); if (doesSubCategoryExists.Count() > 0) { //Error StatusMessage = "Foutmelding : Sub Categorie bestaat reeds onder categorie " + doesSubCategoryExists.First().Category.Name + ". Kies een andere naam aub."; } else { _db.SubCategory.Add(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewModel modelVM = new SubCategoryAndCategoryViewModel() { 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)); }
//GET - EDIT public async Task <IActionResult> Edit(int?id) { if (id == null) { return(NotFound()); } else { var subCategory = await _db.SubCategory.FirstOrDefaultAsync(z => z.Id == id); if (subCategory == null) { return(NotFound()); } else { SubCategoryAndCategoryViewModel modelVM = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = subCategory, SubCategoryList = await _db.SubCategory.OrderBy(z => z.Name).Select(z => z.Name).ToListAsync() }; return(View(modelVM)); } } }
public async Task <IActionResult> Edit(SubCategoryAndCategoryViewModel model) { if (ModelState.IsValid) { var doesSubCategoryExists = await _db.SubCategory.Include(z => z.Category).Where(z => z.Name == model.SubCategory.Name && z.Category.Id == model.SubCategory.CategoryId).ToListAsync(); if (doesSubCategoryExists.Count() > 0) { //Error StatusMessage = "Error : Sub Category exists under " + doesSubCategoryExists.First().Category.Name + " category. Please use another name."; } else { var subCategoryFromDatabase = await _db.SubCategory.FindAsync(model.SubCategory.Id); subCategoryFromDatabase.Name = model.SubCategory.Name; await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCategoryAndCategoryViewModel modelVM = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = model.SubCategory, SubCategoryList = await _db.SubCategory.OrderBy(z => z.Name).Select(z => z.Name).ToListAsync(), StatusMessage = StatusMessage }; return(View(modelVM)); }
public async Task <IActionResult> DeleteConfirmed(int id, SubCategoryAndCategoryViewModel model) { var subCategory = await _db.SubCategory.SingleOrDefaultAsync(m => m.Id == id); _db.SubCategory.Remove(subCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoriesList = await db.Categories.ToListAsync(), SubCategory = new Models.SubCategory() }; return(View(model)); }
//GET CREATE public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel categoryAndCategoryVM = new SubCategoryAndCategoryViewModel() { CategoryList = await context.Categories.ToListAsync(), SubCategory = new Models.SubCategory(), SubCategoryList = await context.SubCategories.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(categoryAndCategoryVM)); }
public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { 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)); }
//GET Action for Create public IActionResult Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = _db.Category.ToList(), SubCategory = new SubCategory(), SubCategoryList = _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToList() }; return(View(model)); }
public async Task <IActionResult> Create() { var subCategoryAndCategoryViewModel = new SubCategoryAndCategoryViewModel() { SubCategory = new SubCategory(), CategoryList = await _db.Category.ToListAsync(), SubCategoryList = await _db.SubCategory.OrderBy(o => o.Name).Select(s => s.Name).Distinct().ToListAsync() }; return(View(subCategoryAndCategoryViewModel)); }
// GET - Details public async Task <IActionResult> Details(int?id) { var model = new SubCategoryAndCategoryViewModel { CategoryList = await db.Category.ToListAsync(), SubCategory = await db.SubCategory.FindAsync(id), SubCategoryList = await db.SubCategory.OrderBy(x => x.Name).Select(x => x.Name).Distinct().ToListAsync() }; return(View(model)); }
//GET - CREATE public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await _unitOfWork.Category.GetAll(), SubCategory = new SubCategory(), SubCategoryList = await _unitOfWork.SubCategory.GetAll() }; return(View(model)); }
private async Task <SubCategoryAndCategoryViewModel> FetchSubCategoryAndCategoryViewModel() { var viewModel = new SubCategoryAndCategoryViewModel() { CategoryList = await _dbContext.Category.ToListAsync(), SubCategory = new SubCategory(), SubCategoryList = await _dbContext.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(viewModel); }
//GET-CREATE public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { //We need to create an extension method to covert the Category object to a select list. This is done in the extensions folder. 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)); }
//GET - CREATE public async Task <IActionResult> Create() { // ViewBag.CategoryList = new SelectList(_db.Categories, "Id", "Name"); SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Categories.ToListAsync(), SubCategory = new Models.SubCategory(), SubCategoryList = await _db.SubCategories.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }
// GET - Create public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync().ConfigureAwait(false), SubCategory = new Models.SubCategory(), // Get subcategories ordered by name, return each subcategory name, return only distinct records SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync().ConfigureAwait(false) }; return(View(model)); }
public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { Categories = await dbContext.Categories.ToListAsync(), SubCategory = new SubCategory(), SubCategoriesExist = await dbContext.SubCategories .Distinct().Select(s => s.SubCategoryName).ToListAsync() }; return(View(model)); }
public IActionResult Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = _dbContext.Categories.ToList(), SubCategory = new Models.SubCategory(), SubCategoryList = _dbContext.SubCategories.OrderBy(i => i.Name) .Select(i => i.Name).Distinct().ToList() // Distinct() aynı isimdekiler gelmesin }; return(View(model)); }
//GET: Create public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await _categoryService.FindAllAsync(), SubCategory = new SubCategory(), SubCategoryList = await _subCategoryService.FindAllNameSCAsync(), StatusMessage = "", }; return(View(model)); }
public async Task <IActionResult> Create() { //Create the ViewModel and populate it from DB which has been dependency injected. var model = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = new Models.SubCategory(), SubCategoryList = await _db.SubCategory.OrderBy(n => n.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }
public async Task <IActionResult> Create(SubCategoryAndCategoryViewModel model) { if (ModelState.IsValid) { var doesSubCategoryExists = _db.SubCategory.Where(s => s.Name == model.SubCategory.Name).Count(); var doesSubCatAndCatExists = _db.SubCategory .Where(s => s.Name == model.SubCategory.Name && s.CategoryId == model.SubCategory.CategoryId).Count(); if (doesSubCategoryExists > 0 && model.isNew) { //error StatusMessage = "Error : Sub Category Name already Exists"; } else { if (doesSubCategoryExists == 0 && !model.isNew) { //error StatusMessage = "Error : Sub Category does not exists"; } else { if (doesSubCatAndCatExists > 0) { //error StatusMessage = "Error : Category and sub category combination exists"; } else { _db.Add(model.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } } } SubCategoryAndCategoryViewModel modelVM = new SubCategoryAndCategoryViewModel() { CategoryList = _db.Category.ToList(), SubCategory = model.SubCategory, SubcategoryList = _db.SubCategory.OrderBy(p => p.Name) .Select(p => p.Name) .ToList(), StatusMessage = StatusMessage }; return(View(modelVM)); }
//GET - CREATE public async Task <IActionResult> Create() { SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = new Models.SubCategory(), //order the sub cat in the db by name, select all of them by name, then get only the names SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }
public async Task <IActionResult> Create() { var subcategoriesList = await subCategoryService.GetAllSubCategories(); SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await categoryService.GetAllCategories(), SubCategory = new SubCategory(), SubCategoryList = subcategoriesList.OrderBy(n => n.Name).Distinct().Select(s => s.Name).ToList() }; return(View(model)); }
public async Task <IActionResult> DeleteConfirmed(SubCategoryAndCategoryViewModel model) { var subCatFromDb = await _db.SubCategory.FindAsync(model.SubCategory.Id); if (subCatFromDb == null) { return(View()); } _db.SubCategory.Remove(subCatFromDb); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
//GET - CREATE //can initialize the new viewmodel inside (public SubCategoryController(ApplicationDbContext db)) public async Task <IActionResult> Create() { //create View Model Object SubCategoryAndCategoryViewModel model = new SubCategoryAndCategoryViewModel() { CategoryList = await _db.Category.ToListAsync(), SubCategory = new SubCategory(), //a list of string only, so we use Select(p => p.Name) SubCategoryList = await _db.SubCategory.OrderBy(p => p.Name).Select(p => p.Name).Distinct().ToListAsync() }; return(View(model)); }