public JsonResult OnGetListAsync(string q, int?page) { var query = (from a in _context.Country select a ); if (!string.IsNullOrWhiteSpace(q)) { query = query.Where(u => u.Name.Contains(q)); } X.PagedList.IPagedList <Country> pagedCountries = query.ToPagedList(page ?? 1, 30); List <Select2ResultDTO> result = new List <Select2ResultDTO>(); foreach (var item in pagedCountries) { Select2ResultDTO temp = new Select2ResultDTO(); temp.id = item.Id.ToString(); temp.text = item.Name; result.Add(temp); } Select2PaginateDTO select2PaginateDTO = new Select2PaginateDTO(); select2PaginateDTO.more = pagedCountries.HasNextPage; Select2DTO select2DTO = new Select2DTO(); select2DTO.results = result; select2DTO.paginate = select2PaginateDTO; return(new JsonResult(select2DTO)); }
public Select2DTO GetAirport(string q, int?page) { try { var query = db.Airport.AsQueryable(); if (!String.IsNullOrWhiteSpace(q)) { query = query.Where(e => e.Name.Contains(q) || e.Icao.Contains(q) || e.Iata.Contains(q)); } X.PagedList.IPagedList <Airport> pagedAirports = query.ToPagedList(page ?? 1, 30); List <Select2ResultDTO> result = new List <Select2ResultDTO>(); foreach (var item in pagedAirports) { Select2ResultDTO temp = new Select2ResultDTO { id = item.Id.ToString(), text = $"{item.Name}" }; result.Add(temp); } Select2PaginateDTO select2PaginateDTO = new Select2PaginateDTO { more = pagedAirports.HasNextPage }; Select2DTO select2DTO = new Select2DTO { results = result, paginate = select2PaginateDTO, }; return(select2DTO); } catch (Exception e) { throw; } }
public Select2DTO SearchItems(string q, int?page) { try { List <Select2ResultDTO> result = new List <Select2ResultDTO>(); foreach (var item in db.Airport.Include(a => a.City.Country.Continent).Where(x => x.Name.Contains(q) || x.Iata.Contains(q) || x.Icao.Contains(q)).ToPagedList(page ?? 1, 30)) { Select2ResultDTO airport = new Select2ResultDTO(); airport.id = "ai_" + item.Id.ToString(); airport.text = item.Name; result.Add(airport); } foreach (var item in db.City.Include(a => a.Country.Continent).Where(x => x.Name.Contains(q)).ToPagedList(page ?? 1, 30)) { Select2ResultDTO city = new Select2ResultDTO(); city.id = "ci_" + item.Id.ToString(); city.text = item.Name; if (!result.Contains(city)) { result.Add(city); } } foreach (var item in db.Country.Include(a => a.Continent).Where(x => x.Name.Contains(q)).ToPagedList(page ?? 1, 30)) { Select2ResultDTO country = new Select2ResultDTO(); country.id = "co_" + item.Id.ToString(); country.text = item.Name; if (!result.Contains(country)) { result.Add(country); } } if (result.Count() == 0) { foreach (var item in db.Airport.Include(a => a.City.Country.Continent).ToPagedList(page ?? 1, 30)) { Select2ResultDTO airport = new Select2ResultDTO(); airport.id = "ai_" + item.Id.ToString(); airport.text = item.Name; result.Add(airport); } } //if (q != null) //{ // if (q.Length >= 3) // { // var oldTerm = db.SearchTerm.Where(st => st.Keyword == q).FirstOrDefault(); // if (oldTerm != null) // { // oldTerm.Count++; // db.Entry(oldTerm).State = EntityState.Modified; // db.SaveChanges(); // } // else // { // SearchTerm searchTerm = new SearchTerm(); // searchTerm.Keyword = q; // searchTerm.Count = 1; // db.SearchTerm.Add(searchTerm); // db.SaveChanges(); // } // } //} IPagedList <Select2ResultDTO> pagedResult = result.ToPagedList(page ?? 1, 90); Select2PaginateDTO select2PaginateDTO = new Select2PaginateDTO(); select2PaginateDTO.more = pagedResult.HasNextPage; Select2DTO select2DTO = new Select2DTO(); select2DTO.results = pagedResult.ToList(); select2DTO.paginate = select2PaginateDTO; return(select2DTO); } catch (Exception e) { Select2DTO select2DTO = new Select2DTO(); var Log = new Log() { Message = e.Message, }; db.Log.Add(Log); db.SaveChanges(); return(select2DTO); //throw; } }