0

I need to do something like: Show list of countries >> Select country -> show list of Cities.

So I have to get a list of cities by country but without using data in database.

Can anyone please suggest me a solution ?. I really appreciate your help.

HungDQ
  • 315
  • 1
  • 5
  • 15
  • You're gonna need some kind of "database" to store the countries and cities, even if it's a CSV file. – Roger Lipscombe Mar 15 '16 at 08:14
  • Or you could use a remote API (Xml or Json web service for instance). Hard coding everything in your controlelr, or even in your cshtml would also work, but you don't want to to that right ?! – kall2sollies Mar 15 '16 at 08:16
  • Yes, my boss request me to do it by something like Culture Info ( The way I get the list of countries ). – HungDQ Mar 15 '16 at 08:18
  • http://stackoverflow.com/questions/2606959/how-to-store-data-without-using-database-and-how-to-retrieve-them – Vibhesh Kaul Mar 15 '16 at 08:26

3 Answers3

2

You could use an API but the problem is that you will have a request everytime your page load. Not every API provider will allow this. For example, Here is an API that gets Country / Cities.

Another solution as you are using .NET technologies is to use a localDB. A localDB is in fact a database but within your app. Have a look to the definition on the MSDN :

It is very easy to install and requires no management, yet it offers the same T-SQL language, programming surface and client-side providers as the regular SQL Server Express. If the simplicity (and limitations) of LocalDB fit the needs of the target application environment, developers can continue using it in production, as LocalDB makes a pretty good embedded database too.

Finely, the last solution that comes in mind if you can't use XML or JSON files nor a LocalDB is to have your lists in classes but in my opinion you should avoid this solution, it will simply load everything in RAM until you application stops, as HDD cost less that RAM I really think the better option is to use XML or JSON files in your app.

  • the link you have provided is of opentracker which gives you geolocation data of visitors to the site and is not at all for the application intended in this question. – Syed Nouman Aug 07 '23 at 07:06
1

You can store the info into a text file or even into a static class in your code (not exactly a great idea, but doable).

Then you just need to get the info from the container and build two SelectList items, one for countries and one for cities.

Use javascript to link change event of countries SelectList to a filtered reload of cities SelectList

Bardo
  • 2,470
  • 2
  • 24
  • 42
  • I understand that solution but my boss force me to implement without using database or text file. – HungDQ Mar 15 '16 at 08:17
  • If you cannot use text file neither go to the hardcoded option, create two linked data structures into your code and load the data into them manually, then use them to fill the SelectList. It's awfully wrong to do that, but if you cannot use a database neither a text file... – Bardo Mar 15 '16 at 08:20
  • Is there any Web Service / API can help me do that ? – HungDQ Mar 15 '16 at 08:22
  • Of course, if you know about an external service that provide you updated info about countries/cities it's a much better option to use it than going hardcode. I didn't know any, usually use a database for this kind of things. – Bardo Mar 15 '16 at 08:24
  • Maybe this could help you http://stackoverflow.com/questions/1794833/list-of-cities-by-country – Bardo Mar 15 '16 at 08:25
  • I hope it, as I worked in Magento, these directory information stored in database so it easily to work. Anyway, thank you very much. – HungDQ Mar 15 '16 at 08:25
0

Assuming you have a preset list of cities by country, and you really cannot use any sort of database, then perhaps just use text files? One text file for the list of countries and then one file per country with the list of cities. Read in the text file and display as needed.

Bryan Lewis
  • 5,629
  • 4
  • 39
  • 45