public async Task <IActionResult> Create(SubCatandCatVm CatandCat) { if (ModelState.IsValid) { var doesSubCatExist = await _db.Subcategory.Include(s => s.Category) .Where(s => s.Name == CatandCat.SubCategory.Name && s.Category.Id == CatandCat.SubCategory.CategoryId) .ToListAsync(); if (doesSubCatExist.Count() > 0) { StatusMessage = "Sub Category exist under " + doesSubCatExist.First().Category.Name; } else { await _db.Subcategory.AddAsync(CatandCat.SubCategory); await _db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } } SubCatandCatVm model = new SubCatandCatVm() { Categories = await _db.Category.ToListAsync(), SubCategory = CatandCat.SubCategory, SubCategoryList = await _db.Subcategory.OrderBy(p => p.Name).Select(p => p.Name).ToListAsync(), StatusMessage = StatusMessage }; return(View(model)); }
public async Task <IActionResult> Create() { SubCatandCatVm subcatVm = new SubCatandCatVm() { Categories = await _db.Category.ToListAsync(), SubCategory = new Models.SubCategory(), SubCategoryList = await _db.Subcategory.OrderBy(p => p.Name).Select(n => n.Name).Distinct().ToListAsync(), StatusMessage = null }; return(View(subcatVm)); }
public async Task <IActionResult> Edit(int id) { var SubCategory = await _db.Subcategory.Where(c => c.Id == id).FirstOrDefaultAsync(); SubCatandCatVm subcatVm = new SubCatandCatVm() { Categories = await _db.Category.ToListAsync(), SubCategory = SubCategory, SubCategoryList = await _db.Subcategory.OrderBy(p => p.Name).Select(n => n.Name).Distinct().ToListAsync(), StatusMessage = null }; return(View(subcatVm)); }
public async Task <IActionResult> Update(SubCatandCatVm subCat) { if (ModelState.IsValid) { var subcategory = await _db.Subcategory.Where(s => s.Id == subCat.SubCategory.Id).FirstOrDefaultAsync(); subcategory.Name = subCat.SubCategory.Name; subcategory.CategoryId = subCat.SubCategory.CategoryId; await _db.SaveChangesAsync(); TempData["success"] = "The update was successful"; return(Redirect(nameof(Edit) + "/" + subCat.SubCategory.Id)); } SubCatandCatVm modelone = new SubCatandCatVm() { StatusMessage = "Error: An error has occured" }; return(RedirectToAction(nameof(Edit))); }