// GET: Products public ActionResult Index(int?id, int?complianceformID, int?compid) { var viewModel = new ProductIndexData(); if (compid != null) { int?updprodid = id; int?upcompid = compid; AddComplianceProduct(updprodid, upcompid, 1); } viewModel.Products = db.Products .Include(i => i.ComplianceForms.Select(c => c.ComplianceCategory)) .Where(i => i.ProductListingID == id.Value) .OrderBy(i => i.ProductName); if (id != null) { ViewBag.ProductID = id.Value; viewModel.ComplianceForms = viewModel.Products.Where( i => i.ProductListingID == id.Value).Single().ComplianceForms; } if (complianceformID != null) { ViewBag.complianceformID = complianceformID.Value; // Lazy loading //viewModel.Enrollments = viewModel.Courses.Where( // x => x.CourseID == courseID).Single().Enrollments; // Explicit loading var selectedcomplianceform = viewModel.ComplianceForms.Where(x => x.ComplianceFormID == complianceformID).Single(); db.Entry(selectedcomplianceform).Collection(x => x.Compliances).Load(); foreach (Compliance compliance in selectedcomplianceform.Compliances) { db.Entry(compliance).Reference(x => x.ComplianceItem).Load(); } viewModel.Compliances = selectedcomplianceform.Compliances; } var productcomp = (from p in db.ProductCompliance where p.ProductListingID == id select p); ViewBag.productcp = productcomp.ToList(); var proditems = from a in db.ProductCompliance select a; ViewBag.ProdItems = new SelectList(proditems, "ProductListingID", "ProductName"); return(View(viewModel)); }
// GET: Products public ActionResult Index(string Compliance_Sorting_Order, string Search_Data, int?id, int?complianceformID, int?compid, int?page) { int pageSize = 14; int pageNumber = (page ?? 1); ViewBag.CurrentSort = Compliance_Sorting_Order; ViewBag.SortingCompliance = String.IsNullOrEmpty(Compliance_Sorting_Order) ? "ComplianceForm" : ""; // ViewBag.SortingCompliance = Compliance_Sorting_Order == "ComplianceForm"; var viewModel = new ProductIndexData(); if (compid != null) { int?updprodid = id; int?upcompid = compid; AddComplianceProduct(updprodid, upcompid, 1); } switch (Compliance_Sorting_Order) { case "ComplianceForm": viewModel.Products = db.Products .Include(i => i.ComplianceForms.Select(c => c.ComplianceCategory)) .OrderByDescending(i => i.ComplianceForms.Count); break; default: viewModel.Products = db.Products.Where(i => i.ComplianceForms.Count >= 1) .Include(i => i.ComplianceForms.Select(c => c.ComplianceCategory)) .OrderBy(i => i.ProductName); break; } if (Search_Data != null) { viewModel.Products = db.Products.Where(i => i.ProductName == Search_Data); } // viewModel.Products = db.Products // .Include(i => i.ComplianceForms.Select(c => c.ComplianceCategory)) // .OrderBy(i => i.ProductName); if (id != null) { ViewBag.ProductID = id.Value; viewModel.ComplianceForms = viewModel.Products.Where( i => i.ProductListingID == id.Value).Single().ComplianceForms; } if (complianceformID != null) { ViewBag.complianceformID = complianceformID.Value; // Lazy loading //viewModel.Enrollments = viewModel.Courses.Where( // x => x.CourseID == courseID).Single().Enrollments; // Explicit loading var selectedcomplianceform = viewModel.ComplianceForms.Where(x => x.ComplianceFormID == complianceformID).Single(); db.Entry(selectedcomplianceform).Collection(x => x.Compliances).Load(); foreach (Compliance compliance in selectedcomplianceform.Compliances) { db.Entry(compliance).Reference(x => x.ComplianceItem).Load(); } viewModel.Compliances = selectedcomplianceform.Compliances; } var productcomp = (from p in db.ProductCompliance where p.ProductListingID == id select p); ViewBag.productcp = productcomp.ToList(); var proditems = from a in db.ProductCompliance select a; ViewBag.ProdItems = new SelectList(proditems, "ProductListingID", "ProductName"); decimal totalPages = ((decimal)(viewModel.Products.Count() / (decimal)pageSize)); ViewBag.TotalPages = Math.Ceiling(totalPages); viewModel.Products = viewModel.Products.ToPagedList(pageNumber, pageSize); ViewBag.OnePageOfProducts = viewModel.Products; ViewBag.PageNumber = pageNumber; return(View(viewModel)); }
// GET: Products public ActionResult Index(string sortOrder, string currentFilter, string searchString, int?page, List <string> areChecked, string catSelected) { List <string> SubCategoriesListChecked = new List <string>(); AreChecked = areChecked; ViewBag.Subcategories = from item in _context.SubCategories orderby item.Name select item.Name ; ViewBag.CurrentSort = sortOrder; ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; if (searchString != null) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; var viewModel = new ProductIndexData(); viewModel.Products = _context.Products .Include(i => i.ProductSubcategories) .ThenInclude(i => i.SubCategory).AsNoTracking(); if (!String.IsNullOrEmpty(searchString)) { viewModel.Products = viewModel.Products.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper())); } //filtro por categoria if (!String.IsNullOrEmpty(catSelected)) { if (catSelected == "Químico" || catSelected == "Quimico") { viewModel.Products = viewModel.Products.Where(s => s.Category == Category.Químico); } else { viewModel.Products = viewModel.Products.Where(s => s.Category == Category.Agrícola); } } //filtro por subcategoria if (AreChecked.Any()) { //var test = AreChecked.ToString(); foreach (var item in AreChecked) { viewModel.Products = from product in viewModel.Products where product.ProductSubcategories.Any(s => s.SubCategory.Name.Contains(item)) select product; } } switch (sortOrder) { case "name_desc": viewModel.Products = viewModel.Products.OrderByDescending(s => s.Name); break; case "Date": viewModel.Products = viewModel.Products.OrderBy(s => s.Category); break; case "date_desc": viewModel.Products = viewModel.Products.OrderByDescending(s => s.Category); break; default: viewModel.Products = viewModel.Products.OrderBy(s => s.Name); break; } int pageSize = 6; int pageNumber = (page ?? 1); return(View(viewModel.Products.ToPagedList(pageNumber, pageSize))); }
public async Task OnGetAsync(string sortOrder, string catFilter, int?productId, string currentFilter, string searchString, string subCatFilter, List <string> areChecked) { List <string> SubCategoriesListChecked = new List <string>(); CurrentSort = sortOrder; var cat = CatFilter; NameSort = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; CatSort = sortOrder == "CatSort" ? "CatSort_desc" : "CatSort"; if (searchString == null) { searchString = currentFilter; } //filtro por categoria CatFilter = catFilter; //filtro por subcategoria SubCatFilter = subCatFilter; //filtro por nome CurrentFilter = searchString; Product = new ProductIndexData(); Product.SubCategories = await _context.SubCategories.OrderBy(i => i.Name).AsNoTracking().ToListAsync(); Product.Products = await _context.Products .Include(i => i.ProductSubcategories) .ThenInclude(i => i.SubCategory) .AsNoTracking() .ToListAsync(); if (productId != null) { ProductId = productId.Value; } //filtro por nome if (!String.IsNullOrEmpty(searchString)) { Product.Products = Product.Products.Where(s => s.Name.ToUpper().Contains(searchString.ToUpper())); } //filtro por categoria if (!String.IsNullOrEmpty(CatFilter)) { if (CatFilter == "Químico" || CatFilter == "Quimico") { Product.Products = Product.Products.Where(s => s.Category == Category.Químico); } else { Product.Products = Product.Products.Where(s => s.Category == Category.Agrícola); } } AreChecked = areChecked; //filtro por subcategoria if (AreChecked.Any()) { //parei aqui var query = await _context.SubCategories.Where(s => AreChecked.Contains(s.Name)).AsNoTracking().ToListAsync(); //Product.Products = Product.Products.Where(i => i.ProductSubcategories.Any(x => x.SubCategory.Name.Contains(SubCatFilter))); //Product.Products = Product.Products.Where(i => i.ProductSubcategories.Any(x => x.SubCategory.Name.Contains(AreChecked.ToString()))); // var samurai = _context.Samurais.Include(s => s.SecretIdentity) //.FirstOrDefault(s => s.Id == 9); //samurai id // samurai.SecretIdentity.RealName = "NovoNome2"; // _context.SaveChanges(); var test = AreChecked.ToString(); foreach (var item in AreChecked) { Product.Products = from product in Product.Products where product.ProductSubcategories.Any(s => s.SubCategory.Name.Contains(item)) select product; } //i.ProductSubcategories.Any(x => x.SubCategory.Name.Contains(SubCatFilter))); } //sorting switch (sortOrder) { case "name_desc": Product.Products = Product.Products.OrderByDescending(s => s.Name); break; case "CatSort": Product.Products = Product.Products.OrderBy(s => s.Category); break; case "CatSort_desc": Product.Products = Product.Products.OrderByDescending(s => s.Category); break; default: Product.Products = Product.Products.OrderBy(s => s.Name); break; } }
public ActionResult Index(string sortOrder, string currentFilter, string searchString, string categoryFilter, string MinPrice, string MaxPrice, int?page) { ViewBag.CurrentSort = sortOrder; ViewBag.PriceHighToLowSortParm = (String.IsNullOrEmpty(sortOrder) || sortOrder == "PriceLowtoHigh") ? "PriceHightoLow" : ""; ViewBag.PriceLowToHighSortParm = (String.IsNullOrEmpty(sortOrder) || sortOrder == "PriceHightoLow") ? "PriceLowtoHigh" : ""; ViewBag.CategoryFilterParm = categoryFilter; ProductIndexData viewmodel = new ProductIndexData(); viewmodel.Products = new AppService().GetProducts(); viewmodel.Categories = new CategoryService().GetCategories(); if (!String.IsNullOrEmpty(categoryFilter)) { var category = viewmodel.Categories.FirstOrDefault(c => c.Name.Contains(categoryFilter)); if (category.ParentCategoryID != null) { viewmodel.Products = category.Products; } else { var subcategories = new CategoryService().GetSubCategories(category.ID); List <Product> productssub = new List <Product>(); foreach (var item in subcategories) { productssub.AddRange(item.Products); } viewmodel.Products = productssub; } } if (!String.IsNullOrEmpty(searchString)) { page = 1; } else { searchString = currentFilter; } ViewBag.CurrentFilter = searchString; if (!String.IsNullOrEmpty(searchString)) { viewmodel.Products = viewmodel.Products.Where(s => s.Name.ToLower().Contains(searchString.ToLower()) || s.Description.ToLower().Contains(searchString.ToLower()) || s.Category.Name.ToLower().Contains(searchString.ToLower())); } ViewBag.PriceRangeMinPrice = MinPrice; ViewBag.PriceRangeMaxPrice = MaxPrice; if (!String.IsNullOrEmpty(MinPrice) && !String.IsNullOrEmpty(MaxPrice)) { viewmodel.Products = viewmodel.Products.Where(s => s.Price >= Convert.ToDecimal(MinPrice) && s.Price <= Convert.ToDecimal(MaxPrice)); } else if (!String.IsNullOrEmpty(MinPrice) && String.IsNullOrEmpty(MaxPrice)) { viewmodel.Products = viewmodel.Products.Where(s => s.Price >= Convert.ToDecimal(MinPrice)); } else if (String.IsNullOrEmpty(MinPrice) && !String.IsNullOrEmpty(MaxPrice)) { viewmodel.Products = viewmodel.Products.Where(s => s.Price <= Convert.ToDecimal(MaxPrice)); } else { viewmodel.Products = viewmodel.Products; } switch (sortOrder) { case "PriceHightoLow": viewmodel.Products = viewmodel.Products.OrderByDescending(s => s.Price); break; case "PriceLowtoHigh": viewmodel.Products = viewmodel.Products.OrderBy(s => s.Price); break; default: viewmodel.Products = viewmodel.Products.OrderBy(s => s.ID); break; } int pageSize = 12; int pageNumber = (page ?? 1); viewmodel.Products = viewmodel.Products.ToPagedList(pageNumber, pageSize); return(View(viewmodel)); }
// GET: Products public ActionResult Index(int?id, int?complianceformID, int?compid, bool?enditem, int?compidsub) { var viewModel = new ProductIndexData(); if (compid != null) { int?updprodid = id; int?upcompid = compid; AddComplianceProduct(updprodid, upcompid, 1, enditem); } viewModel.Products = db.Products .Include(i => i.ComplianceForms.Select(c => c.ComplianceCategory)) .Where(i => i.ProductListingID == id.Value) .OrderBy(i => i.ProductName); if (id != null) { int idval = 0; idval = id.GetValueOrDefault(); if (CheckRelProductComplianceForms(idval) > 0) { ViewBag.ProductID = id.Value; viewModel.ComplianceForms = viewModel.Products.Where( i => i.ProductListingID == id.Value).FirstOrDefault().ComplianceForms; } else { try { ViewBag.ProductID = id.Value; viewModel.ComplianceForms = viewModel.Products.Where(i => i.ProductListingID == id.Value).FirstOrDefault().ComplianceForms; } catch (Exception ex) { } } } if (complianceformID != null) { ViewBag.complianceformID = complianceformID.Value; // Lazy loading //viewModel.Enrollments = viewModel.Courses.Where( // x => x.CourseID == courseID).Single().Enrollments; // Explicit loading var selectedcomplianceform = viewModel.ComplianceForms.Where(x => x.ComplianceFormID == complianceformID).Single(); db.Entry(selectedcomplianceform).Collection(x => x.Compliances).Load(); foreach (Compliance compliance in selectedcomplianceform.Compliances) { db.Entry(compliance).Reference(x => x.ComplianceItem).Load(); } viewModel.Compliances = selectedcomplianceform.Compliances.OrderBy(x => x.Order); } //Product compliance items var productcomp = (from p in db.ProductCompliance where p.ProductListingID == id select p); ViewBag.productcp = productcomp.ToList(); //Section to handle subordinate items var productSubItem = from a in db.ComplianceItems join s in db.ComplianceItemSubItems on a.ComplianceItemsID equals s.SubItemTo // join p in db.ProductCompliance on s.SubItemTo equals p.ComplianceItemsID //select new SubItemsViewModel { SubItemTo = s.SubItemTo, ItemName = a.ItemName, ComplianceItemsID = s.ComplianceItemID, Checked = p.Checked }; select new SubItemsViewModel { SubItemTo = s.SubItemTo, ItemName = a.ItemName, ComplianceItemsID = s.ComplianceItemID }; if (productSubItem.Count() > 1) { ViewBag.productSubItem = productSubItem.ToList(); } //check if subordinate item is in the product compliances //will end up here as another item var subproductcomp = (from p in db.ProductCompliance join s in db.ComplianceItemSubItems on p.ComplianceItemsID equals s.SubItemTo //where p.ProductListingID == id select new SubItemsProductCompliance { ComplianceItemsID = s.ComplianceItemID, SubItemTo = s.SubItemTo, ProductListingID = p.ProductListingID, Checked = p.Checked }); ViewBag.productcpsub = subproductcomp.ToList(); //end section to handle subordinate itesm var proditems = from a in db.ProductCompliance select a; ViewBag.ProdItems = new SelectList(proditems, "ProductListingID", "ProductName"); return(View(viewModel)); }