//public ActionResult Index(string SelectedColumn, string ascending, string searchName, string selectedVehicleType) public ActionResult Index() { IQueryable <ParkedVehicle> parkedVehicles = db.ParkedVehicle; ParkedVehiclesViewModel model = new ParkedVehiclesViewModel { ColumnSelectList = GetColumnSelectList(), ParkedVehicles = parkedVehicles }; return(View(model)); }
// GET: ParkedVehicles public async Task <IActionResult> Index() { var vehicles = await _context.ParkedVehicle.ToListAsync(); var vTypes = await GetTypeAsync(); var model = new ParkedVehiclesViewModel { ParkedVehicles = vehicles, Types = vTypes }; return(View(nameof(Index), model)); }
public IActionResult FilterDetailedList(ParkedVehiclesViewModel viewModel) { var vehicles = string.IsNullOrWhiteSpace(viewModel.RegNr) ? _context.ParkedVehicle : _context.ParkedVehicle.Where(m => m.RegNr.StartsWith(viewModel.RegNr)); vehicles = viewModel.VehicleType == null ? vehicles : vehicles.Where(V => V.VehicleType == viewModel.VehicleType); var model = new ParkedVehiclesViewModel { ParkedVehicles = vehicles }; return(View(nameof(Index), model)); }
//public ActionResult Index(string SelectedColumn, string ascending, string searchName, string selectedVehicleType) public ActionResult All([Bind(Include = "SearchName,SelectedSorting,SelectedColumn")] ParkedVehiclesViewModel model) { IQueryable <ParkedVehicle> parkedVehicles; switch (model.SelectedColumn) { case "Customer": //Havent implemented sorting with name yet, a little more complex with joins needed //This code can probably be simplified //When there is no search name entere wwe just return a list if (String.IsNullOrWhiteSpace(model.SearchName)) { parkedVehicles = db.ParkedVehicle; } else { var splitted = model.SearchName.Split(' '); string FirstName, LastName; if (splitted.Length != 2) { //A bit ugly solution should somehow just return an empty Iqueryable list FirstName = ""; LastName = ""; } else { FirstName = splitted[0]; LastName = splitted[1]; } var member = db.Member.Where(m => m.FirstName.ToLower() == FirstName.ToLower() && m.LastName.ToLower() == LastName.ToLower()); parkedVehicles = db.ParkedVehicle.Where(v => v.MembersId == member.FirstOrDefault().Id); } break; case "RegNr": parkedVehicles = (!String.IsNullOrEmpty(model.SearchName)) ? db.ParkedVehicle.Where(v => v.RegistrationNumber.Equals(model.SearchName)) : db.ParkedVehicle; parkedVehicles = model.SelectedSorting.Equals("Descending") ? parkedVehicles.OrderByDescending(v => v.RegistrationNumber) : parkedVehicles.OrderBy(v => v.RegistrationNumber); break; case "VehicleType": parkedVehicles = (!String.IsNullOrEmpty(model.SearchName)) ? db.ParkedVehicle.Where(v => v.VehicleType.Type.Equals(model.SearchName)) : db.ParkedVehicle; parkedVehicles = model.SelectedSorting.Equals("Descending") ? parkedVehicles.OrderByDescending(v => v.VehicleType.Type) : parkedVehicles.OrderBy(v => v.VehicleType.Type); break; case "Color": parkedVehicles = (!String.IsNullOrEmpty(model.SearchName)) ? db.ParkedVehicle.Where(v => v.Color.Equals(model.SearchName)) : db.ParkedVehicle; parkedVehicles = !Ascending(ViewBag.Ascending) ? parkedVehicles.OrderByDescending(v => v.Color) : parkedVehicles.OrderBy(v => v.Color); break; case "Brand": parkedVehicles = (!String.IsNullOrEmpty(model.SearchName)) ? db.ParkedVehicle.Where(v => v.Brand.Equals(model.SearchName)) : db.ParkedVehicle; parkedVehicles = !Ascending(ViewBag.Ascending) ? parkedVehicles.OrderByDescending(v => v.Brand) : parkedVehicles.OrderBy(v => v.Brand); break; case "Wheels": parkedVehicles = (!String.IsNullOrEmpty(model.SearchName)) ? db.ParkedVehicle.Where(v => v.Wheels.ToString().Equals(model.SearchName)) : db.ParkedVehicle; parkedVehicles = !Ascending(ViewBag.Ascending) ? parkedVehicles.OrderByDescending(v => v.Wheels) : parkedVehicles.OrderBy(v => v.Wheels); break; default: parkedVehicles = (!String.IsNullOrEmpty(model.SearchName)) ? db.ParkedVehicle.Where(v => v.ParkingTime.ToString().Contains(model.SearchName)) : db.ParkedVehicle; parkedVehicles = !Ascending(ViewBag.Ascending) ? parkedVehicles.OrderByDescending(v => v.ParkingTime) : parkedVehicles.OrderBy(v => v.ParkingTime); break; } model.ColumnSelectList = GetColumnSelectList(); model.ParkedVehicles = parkedVehicles; return(View(model)); }
//public ActionResult Index(string SelectedColumn, string ascending, string searchName, string selectedVehicleType) public ActionResult Index([Bind(Include = "SearchName,SelectedSorting,SelectedColumn")] ParkedVehiclesViewModel model) { IQueryable <ParkedVehicle> parkedVehicles; switch (model.SelectedColumn) { case "Customer": //When there is no search name entere we just return a list if (String.IsNullOrWhiteSpace(model.SearchName)) { if (model.SelectedSorting == "Ascending") { parkedVehicles = from member in db.Member join vehicle in db.ParkedVehicle on member.Id equals vehicle.MembersId orderby member.FirstName ascending select vehicle; } else { parkedVehicles = from member in db.Member join vehicle in db.ParkedVehicle on member.Id equals vehicle.MembersId orderby member.FirstName descending select vehicle; } } else { var splitted = model.SearchName.Split(' '); string FirstName, LastName; if (splitted.Length != 2) { //A bit ugly solution should somehow just return an empty Iqueryable list FirstName = ""; LastName = ""; } else { FirstName = splitted[0]; LastName = splitted[1]; } parkedVehicles = from member in db.Member join vehicle in db.ParkedVehicle on member.Id equals vehicle.MembersId orderby member.FirstName where (member.FirstName.ToLower() == FirstName.ToLower() && (member.LastName.ToLower() == LastName.ToLower())) select vehicle; //var member = db.Member.Where(m => m.FirstName.ToLower() == FirstName.ToLower() && m.LastName.ToLower() == LastName.ToLower()); //parkedVehicles = db.ParkedVehicle.Where(v => v.MembersId == member.FirstOrDefault().Id); } break; case "RegNr": parkedVehicles = (!String.IsNullOrEmpty(model.SearchName)) ? db.ParkedVehicle.Where(v => v.RegistrationNumber.Equals(model.SearchName)) : db.ParkedVehicle; parkedVehicles = model.SelectedSorting.Equals("Descending") ? parkedVehicles.OrderByDescending(v => v.RegistrationNumber) : parkedVehicles.OrderBy(v => v.RegistrationNumber); break; default: parkedVehicles = (!String.IsNullOrEmpty(model.SearchName)) ? db.ParkedVehicle.Where(v => v.VehicleType.Type.Equals(model.SearchName)) : db.ParkedVehicle; parkedVehicles = model.SelectedSorting.Equals("Descending") ? parkedVehicles.OrderByDescending(v => v.VehicleType.Type) : parkedVehicles.OrderBy(v => v.VehicleType.Type); break; } model.ColumnSelectList = GetColumnSelectList(); model.ParkedVehicles = parkedVehicles; return(View(model)); }