public async Task <IActionResult> Create(CategoryVM categoryVM) { ViewBag.CategoryAdd = "Failed"; categoryVM.CreatedAt = DateTime.Now; if (categoryVM != null) { var category = CategoryUtility.MapVMtoModel(categoryVM); //stop admin add 3rd category if (category.ParentId == null) { category.Level = 1; } else if (CategoryExists(category.ParentId) == true) { category.Level = 2; } db.Add(category); await db.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(categoryVM)); }
public async Task <IActionResult> Detail(int id) { if (HttpContext.Session.GetInt32("checkidAdmin") != null) { if (id == null) { return(NotFound()); } var category = await db.Categories .Include(c => c.Parent) .FirstOrDefaultAsync(m => m.Id == id); if (category == null) { return(NotFound()); } layoutVM.CategoryVM = CategoryUtility.MapModeltoVM(category); return(View(layoutVM)); } else { return(RedirectToAction("Login", "Account")); } }
public IActionResult SearchCategory(string keyword) { if (!string.IsNullOrEmpty(keyword)) { var checkkeyword = db.Categories.Where(a => a.Name.Trim().Contains(keyword.Trim())).ToList(); if (checkkeyword != null) { var viewmodel = new LayoutViewModel() { CategoriesVM = CategoryUtility.MapModelsToVMs(checkkeyword.ToList()) }; return(View("Index", viewmodel)); } else { return(View("Index")); } } else { return(RedirectToAction("Index")); } }
public async Task <IActionResult> Edit(int id) { if (HttpContext.Session.GetInt32("checkidAdmin") != null) { if (id == null) { return(NotFound()); } var category = await db.Categories.FindAsync(id); layoutVM.CategoryVM = CategoryUtility.MapModeltoVM(category); layoutVM.CategoryVM.Categories = db.Categories.Select(a => new SelectListItem { Value = a.Id.ToString(), Text = a.Name }).ToList(); if (category == null) { return(NotFound()); } ViewData["ParentId"] = new SelectList(db.Categories, "Id", "Id", category.ParentId); return(View(layoutVM)); } else { return(RedirectToAction("Login", "Account")); } }
public async Task <IActionResult> Index() { if (HttpContext.Session.GetInt32("checkidAdmin") != null) { //var categories = db.Categories.Include(c => c.Parent); var categories = db.Categories.Include(c => c.Parent); layoutVM.CategoriesVM = CategoryUtility.MapModelsToVMs(categories.ToList()); return(View(layoutVM)); } else { return(RedirectToAction("Login", "Account")); } }
public async Task <IActionResult> ListedByCategory(int id) { var category = db.Categories.Find(id); //var c = category.Children; //var items = db.Items.FromSqlRaw( // $"Select i.* from Categories c, CategoryItems ci, Items i where c.Id = ci.CategoryId and i.Id = ci.ItemId and c.Id = " + id); var categoryItems = db.CategoryItems.Where(x => x.CategoryId == category.Id).ToList(); foreach (var categoryItem in categoryItems) { categoryItem.Item = db.Items.FirstOrDefault(x => x.Id == categoryItem.ItemId); } category.CategoryItems = categoryItems; layoutVM.CategoryVM = CategoryUtility.MapModeltoVM(category); return(View(layoutVM)); }