public ActionResult Index() { var model = new CategoriesIndexViewModel(); model.Categories = _session.Query <Category>().Where(c => c.ParentCategory == null).ToList(); return(View(model)); }
// GET: Categories public async Task <IActionResult> Index( int?id, int?productID, string sortOrder, string CurrentFilter, string searchString) { ViewData["CurrentSort"] = sortOrder; ViewData["PriceSortParm"] = sortOrder == "price_desc" ? "price_asc" : "price_desc"; ViewData["CurrentFilter"] = searchString; //This is how we load all data into the view (using eager loading) ViewData["CurrentFilter"] = searchString; var viewModel = new CategoriesIndexViewModel(); viewModel.Categories = await _context.Categories .Include(c => c.ProductCategories) .ThenInclude(c => c.Product) .AsNoTracking() .OrderBy(c => c.CategoryName) .ToListAsync(); if (id != null) { ViewData["CategoryID"] = id.Value; Category category = viewModel.Categories.Where(c => c.CategoryID == id.Value).Single(); viewModel.Products = category.ProductCategories.Select(s => s.Product); } if (productID != null) { ViewData["ProductID"] = productID.Value; viewModel.ProductCategories = viewModel.Products.Where(x => x.ProductID == productID).Single().ProductCategories; } if (!String.IsNullOrEmpty(searchString)) { var products = from p in _context.Products select p; viewModel.Products = viewModel.Products.Where(p => p.Description.ToUpper().Contains(searchString.ToUpper())); } switch (sortOrder) { case "price_desc": viewModel.Products = viewModel.Products.OrderByDescending(p => (float)p.Price); ViewData["WhichSort"] = 1; break; case "price_asc": viewModel.Products = viewModel.Products.OrderBy(p => (float)p.Price); ViewData["WhichSort"] = 0; break; default: viewModel.Products = viewModel.Products.OrderBy(p => (float)p.Price); break; } return(View(viewModel)); }
public ActionResult Index() { var model = new CategoriesIndexViewModel(); IQueryable <Category> categories = categoryService.FindAll(); model.Categories = Mapper.Map <List <CategoryIndexViewModel> >(categories); foreach (CategoryIndexViewModel category in model.Categories) { category.ProductsCount = categoryService.GetProductsCount(category.Id); } return(View(model)); }
public IActionResult Index(string q) { var viewModel = new CategoriesIndexViewModel(); viewModel.Categories = _dbContext.Categories.Select(dbCat => new CategoryViewModel { Id = dbCat.Id, Title = dbCat.Title, }).ToList(); return(View(viewModel)); }
public async Task <IActionResult> Index() { var user = await GetCurrentUserAsync(); var model = new CategoriesIndexViewModel(); model.UserCategories = await _context.UserCategories.Include(uc => uc.User).Where(uc => uc.UserId == user.Id).ToListAsync(); model.Categories = await _context.Categories.Include(c => c.User).Where(c => c.UserId == user.Id).OrderBy(c => c.Title).ToListAsync(); if (model.Categories.Count == 0) { return(View("create")); } else { return(View(model)); } }
public JsonResult ListJson() { var model = new CategoriesIndexViewModel(); IQueryable <Category> categories = categoryService.FindAll(); model.Categories = Mapper.Map <List <CategoryIndexViewModel> >(categories); var jsonObject = from c in model.Categories let nameWithParent = model.GetNameWithParent(c.Id) orderby nameWithParent select new { c.Id, c.Name, NameWithParent = nameWithParent }; return(Json(jsonObject, JsonRequestBehavior.AllowGet)); }
public ActionResult Index() { var model = new CategoriesIndexViewModel(); model.Categories = _session.Query<Category>().Where(c => c.ParentCategory == null).ToList(); return View(model); }
public async Task <IActionResult> Index(CategoriesIndexViewModel ViewModel) { var user = await GetCurrentUserAsync(); var model = new CategoriesIndexViewModel(); ViewModel.UserCategories = await _context.UserCategories.Include(uc => uc.User).Where(uc => uc.UserId == user.Id).ToListAsync(); if (ViewModel.Categories.Count == ViewModel.MinutesSpentList.Count) { for (int i = 0; i < ViewModel.Categories.Count; i++) { UserCategory usercategory = new UserCategory { UserId = user.Id, CategoryId = ViewModel.Categories[i].Id, MinutesSpent = ViewModel.MinutesSpentList[i], DatePicked = ViewModel.DatePicked }; for (int j = 0; j < ViewModel.UserCategories.Count; j++) { if (usercategory.DatePicked == ViewModel.UserCategories[j].DatePicked) { ModelState.Remove("Category"); ModelState.Remove("Categories"); ModelState.Remove("Categories.Title"); if (ModelState.IsValid) { bool DatePickedAlreadyEntered = true; if (DatePickedAlreadyEntered) { ModelState.AddModelError(string.Empty, "This date has been entered already. Please select another date."); var model2 = new CategoriesIndexViewModel(); model2.UserCategories = await _context.UserCategories.Include(uc => uc.User).Where(uc => uc.UserId == user.Id).ToListAsync(); model2.Categories = await _context.Categories.Include(c => c.User).Where(c => c.UserId == user.Id).OrderBy(c => c.Title).ToListAsync(); return(View(model2)); } } } //} } _context.Add(usercategory); } await _context.SaveChangesAsync(); return(RedirectToAction("Index", "UserCategories")); } else { ModelState.AddModelError(string.Empty, "Please enter data."); var model3 = new CategoriesIndexViewModel(); model3.UserCategories = await _context.UserCategories.Include(uc => uc.User).Where(uc => uc.UserId == user.Id).ToListAsync(); model3.Categories = await _context.Categories.Include(c => c.User).Where(c => c.UserId == user.Id).OrderBy(c => c.Title).ToListAsync(); return(View(model3)); } }
public JsonResult ListJson() { var model = new CategoriesIndexViewModel(); IQueryable<Category> categories = categoryService.FindAll(); model.Categories = Mapper.Map<List<CategoryIndexViewModel>>(categories); var jsonObject = from c in model.Categories let nameWithParent = model.GetNameWithParent(c.Id) orderby nameWithParent select new { c.Id, c.Name, NameWithParent = nameWithParent }; return Json(jsonObject, JsonRequestBehavior.AllowGet); }
public ActionResult Index() { var model = new CategoriesIndexViewModel(); IQueryable<Category> categories = categoryService.FindAll(); model.Categories = Mapper.Map<List<CategoryIndexViewModel>>(categories); foreach (CategoryIndexViewModel category in model.Categories) { category.ProductsCount = categoryService.GetProductsCount(category.Id); } return View(model); }