public async Task<ActionResult/*HomeViewModels*/> Index() { // Two Models in Single View HomeViewModels viewModel = new HomeViewModels(); // Find all types with number of items //var properties = db.Properties.Where(p => p.Availability.ToString().Contains("Yes")) // .GroupBy(p => p.OfType) // .Select(p => new ofTypeViewModel // { // Type = p.Key.ToString(), // Count = p.Count() // }).ToList(); // Find 3 Latest Feature Properties var allFeatured = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes") && p.Featured.ToString().Equals("Yes")).OrderBy(p => p.Id).Take(3).ToListAsync(); List<HomePropertyPhotoViewModel> allFeaturedPhoto = new List<HomePropertyPhotoViewModel>(); foreach (var property in allFeatured) { HomePropertyPhotoViewModel model = new HomePropertyPhotoViewModel(); model.Property = property; model.Photo = await db.Photos.Where(ph => ph.PropertyId == property.Id).OrderBy(ph => ph.Id).Take(1).SingleOrDefaultAsync(); //model.Products = db.Photos.Where(ph => ph.PropertyID == property.ID).ToList(); allFeaturedPhoto.Add(model); } viewModel.FeaturePropertyPhoto = allFeaturedPhoto.ToList(); // Find all Types with related SubTypes //var types = db.OfSubTypes.Include(o => o.OfType).OrderBy(o => o.OfTypeID).ToList(); //viewModel.Types = types; var allTypeSubTypes = await db.OfTypes.ToListAsync(); List<HomeTypeSubTypesViewModel> allTypeSubTypesL = new List<HomeTypeSubTypesViewModel>(); foreach (var type in allTypeSubTypes) { HomeTypeSubTypesViewModel model = new HomeTypeSubTypesViewModel(); model.Type = type; model.SubTypes = await db.OfSubTypes.Where(o => o.OfTypeID == type.ID).OrderBy(o => o.ID).ToListAsync(); //model.Products = db.Photos.Where(ph => ph.PropertyID == property.ID).ToList(); allTypeSubTypesL.Add(model); } viewModel.TypeSubTypes = allTypeSubTypesL.ToList(); // Find all For with number of items count var fors = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes")) .GroupBy(p => p.For) .Select(p => new HomeForViewModel { Title = p.Key.ToString(), Count = p.Count() }).ToListAsync(); viewModel.For = fors; // Find all AllowBidding with number of items count var biddings = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes")) .GroupBy(p => p.AllowBidding) .Select(p => new HomeBiddingsViewModel { Status = p.Key.ToString(), Count = p.Count() }).ToListAsync(); viewModel.Biddings = biddings; var allProperties = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes")).OrderByDescending(p => p.Views).Take(6).ToListAsync(); List<HomePropertyPhotoViewModel> allPropertyPhoto = new List<HomePropertyPhotoViewModel>(); foreach (var property in allProperties) { HomePropertyPhotoViewModel model = new HomePropertyPhotoViewModel(); model.Property = property; model.Photo = await db.Photos.Where(ph => ph.PropertyId == property.Id).OrderBy(ph => ph.Id).Take(1).SingleOrDefaultAsync(); //model.Products = db.Photos.Where(ph => ph.PropertyID == property.ID).ToList(); allPropertyPhoto.Add(model); } viewModel.PropertyPhoto = allPropertyPhoto.ToList(); var allRecents = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes") && p.AllowBidding.ToString().Contains("Disabled")).OrderBy(p => p.Id).Take(4).ToListAsync(); List<HomeRecentPhotoViewModel> allRecentPhoto = new List<HomeRecentPhotoViewModel>(); foreach (var property in allRecents) { HomeRecentPhotoViewModel model = new HomeRecentPhotoViewModel(); model.Property = property; model.Photo = await db.Photos.Where(ph => ph.PropertyId == property.Id).OrderBy(ph => ph.Id).Take(1).SingleOrDefaultAsync(); //model.Products = db.Photos.Where(ph => ph.PropertyID == property.ID).ToList(); allRecentPhoto.Add(model); } viewModel.RecentPropertyPhoto = allRecentPhoto.ToList(); var allBiddings = await db.Biddings.Where(b => b.BiddingStatus.ToString().Contains("Active")).OrderByDescending(b => b.Id).Take(4).ToListAsync(); List<BiddingsPhotoViewModel> allBiddingPhoto = new List<BiddingsPhotoViewModel>(); foreach (var bidding in allBiddings) { BiddingsPhotoViewModel model = new BiddingsPhotoViewModel(); model.Bidding = bidding; model.Property = bidding.Property; model.OfSubType = (model.Property.OfSubType != null) ? await db.OfSubTypes.FindAsync(model.Property.OfSubType) : null; model.Photo = await db.Photos.Where(ph => ph.PropertyId == model.Property.Id).OrderBy(ph => ph.Id).Take(1).SingleOrDefaultAsync(); //model.Products = db.Photos.Where(ph => ph.PropertyID == property.ID).ToList(); allBiddingPhoto.Add(model); } viewModel.BiddingPropertyPhoto = allBiddingPhoto.ToList(); return View(viewModel); }
public async Task<ActionResult/*HomeViewModels*/> Properties(string filterType, string filterSuType, string filterFor, string filterBidding, int? filterRangeMi, int? filterRangeMx, int? page) { // Two Models in Single View HomeViewModels viewModel = new HomeViewModels(); // Find all Types with related SubTypes //var types = db.OfSubTypes.Include(o => o.OfType).OrderBy(o => o.OfTypeID).ToList(); //viewModel.Types = types; var allTypeSubTypes = await db.OfTypes.ToListAsync(); List<HomeTypeSubTypesViewModel> allTypeSubTypesL = new List<HomeTypeSubTypesViewModel>(); foreach (var type in allTypeSubTypes) { HomeTypeSubTypesViewModel model = new HomeTypeSubTypesViewModel(); model.Type = type; model.SubTypes = await db.OfSubTypes.Where(o => o.OfTypeID == type.ID).OrderBy(o => o.ID).ToListAsync(); //model.Products = db.Photos.Where(ph => ph.PropertyID == property.ID).ToList(); allTypeSubTypesL.Add(model); } viewModel.TypeSubTypes = allTypeSubTypesL.ToList(); IEnumerable<Property> allProperties = null; //ViewBag.CurrentSort = filterType; //ViewBag.TypeSortParm = String.IsNullOrEmpty(filterType) ? filterType : ""; if (filterType != null && filterSuType != null && page == null) { page = 1; var subType = await db.OfSubTypes.Where(sb => sb.Title.ToString().Equals(filterSuType)).FirstOrDefaultAsync(); allProperties = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes") && p.OfType.Title.ToString().Equals(filterType) && p.OfSubType == subType.ID).OrderBy(p => p.Views).ToListAsync(); } else if (filterType != null && page == null) { page = 1; allProperties = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes") && p.OfType.Title.ToString().Equals(filterType)).OrderBy(p => p.Views).ToListAsync(); } else { allProperties = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes")).OrderBy(p => p.Views).ToListAsync(); } if (filterFor != null) { allProperties = allProperties.Where(p => p.For.ToString().Contains(filterFor)).ToList(); } if (filterBidding != null) { allProperties = allProperties.Where(p => p.AllowBidding.ToString().Contains(filterBidding)).ToList(); } if (filterRangeMi != null && filterRangeMx != null) { allProperties = allProperties.Where(p => p.Price > filterRangeMi && p.Price < filterRangeMx).ToList(); } ViewBag.CurrentType = filterType; ViewBag.CurrentSubType = filterSuType; ViewBag.CurrentFor = filterFor; ViewBag.CurrentBidding = filterBidding; ViewBag.CurrentRangeMi = filterRangeMi; ViewBag.CurrentRangeMx = filterRangeMx; // Update Side menu items count according to filter if ((filterType != null && filterSuType != null)) { var subType = await db.OfSubTypes.Where(sb => sb.Title.ToString().Equals(filterSuType)).FirstOrDefaultAsync(); // Find all For with number of items count var fors = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes") && p.OfType.Title.ToString().Equals(filterType) && p.OfSubType == subType.ID) .GroupBy(p => p.For) .Select(p => new HomeForViewModel { Title = p.Key.ToString(), Count = p.Count() }).ToListAsync(); viewModel.For = fors; // Find all AllowBidding with number of items count var biddings = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes") && p.OfType.Title.ToString().Equals(filterType) && p.OfSubType == subType.ID) .GroupBy(p => p.AllowBidding) .Select(p => new HomeBiddingsViewModel { Status = p.Key.ToString(), Count = p.Count() }).ToListAsync(); viewModel.Biddings = biddings; } else if (filterType != null && filterSuType == null) { // Find all For with number of items count var fors = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes") && p.OfType.Title.ToString().Equals(filterType)) .GroupBy(p => p.For) .Select(p => new HomeForViewModel { Title = p.Key.ToString(), Count = p.Count() }).ToListAsync(); viewModel.For = fors; // Find all AllowBidding with number of items count var biddings = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes") && p.OfType.Title.ToString().Equals(filterType)) .GroupBy(p => p.AllowBidding) .Select(p => new HomeBiddingsViewModel { Status = p.Key.ToString(), Count = p.Count() }).ToListAsync(); viewModel.Biddings = biddings; } else if (filterType == null && filterSuType == null && filterFor == null && filterBidding == null && filterRangeMi == null && filterRangeMx == null) { // Find all For with number of items count var fors = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes")) .GroupBy(p => p.For) .Select(p => new HomeForViewModel { Title = p.Key.ToString(), Count = p.Count() }).ToListAsync(); viewModel.For = fors; // Find all AllowBidding with number of items count var biddings = await db.Properties.Where(p => p.Availability.ToString().Contains("Yes")) .GroupBy(p => p.AllowBidding) .Select(p => new HomeBiddingsViewModel { Status = p.Key.ToString(), Count = p.Count() }).ToListAsync(); viewModel.Biddings = biddings; } if (filterFor != null || filterBidding != null || (filterRangeMi != null && filterRangeMx != null)) { if (viewModel.For == null) { // Find all For with number of items count var fors = allProperties.Where(p => p.Availability.ToString().Contains("Yes")) .GroupBy(p => p.For) .Select(p => new HomeForViewModel { Title = p.Key.ToString(), Count = p.Count() }).ToList(); viewModel.For = fors; } if (viewModel.Biddings == null) { // Find all AllowBidding with number of items count var biddings = allProperties.Where(p => p.Availability.ToString().Contains("Yes")) .GroupBy(p => p.AllowBidding) .Select(p => new HomeBiddingsViewModel { Status = p.Key.ToString(), Count = p.Count() }).ToList(); viewModel.Biddings = biddings; } } List<HomePropertyPhotoViewModel> allPropertyPhoto = new List<HomePropertyPhotoViewModel>(); foreach (var property in allProperties) { HomePropertyPhotoViewModel model = new HomePropertyPhotoViewModel(); model.Property = property; model.Photo = await db.Photos.Where(ph => ph.PropertyId == property.Id).OrderBy(ph => ph.Id).Take(1).SingleOrDefaultAsync(); //model.Products = db.Photos.Where(ph => ph.PropertyID == property.ID).ToList(); allPropertyPhoto.Add(model); } int pageSize = 9; int pageNumber = (page ?? 1); viewModel.PropertyPhotoFilterable = allPropertyPhoto.ToPagedList(pageNumber, pageSize); //if (!String.IsNullOrEmpty(filterType)) //{ // properties = properties.Where(p => p.Title.ToUpper().Contains(searchProperty.ToUpper()) // || p.Title.ToUpper().Contains(searchProperty.ToUpper())); //} //return View(properties.ToPagedList(pageNumber, pageSize)); return View(viewModel); }