In my registeration form i have fields named country and city. I want that i select a country lets say USA then city field should show all the cities in USA.
i know that i have to put all the cities in Database and then bind it to dropdown box , putting IF statements etc blah blah !!!
but isn't there some alternative to do it , to save time ? it would take alot of time to code. HELP !!!

Dani AI

Generated

As correctly pointed out, keeping cities in a data source is the right direction. For a tight deadline there are two practical, low-effort options that avoid lots of manual coding while giving a good user experience: preload a small JSON list on the page and filter client-side, or fetch cities on demand with a tiny AJAX call. The former is simplest for small country/city sets; the latter scales better and is fast to implement in ASP.NET.

Example client-side (jQuery) to fetch cities on selection:

$("#country").on("change", function () {
  var countryId = $(this).val();
  $.getJSON("/api/locations/cities/" + countryId, function (cities) {
    var opts = "<option value=''>Select city</option>";
    $.each(cities, function (i, c) { opts += "<option value='" + c.Id + "'>" + c.Name + "</option>"; });
    $("#city").html(opts);
  }).fail(function () { alert("Could not load cities."); });
});

Example server endpoint (ASP.NET Web API style):

[HttpGet]
[Route("api/locations/cities/{countryId:int}")]
public IHttpActionResult GetCities(int countryId)
{
  var list = db.Cities
               .Where(x => x.CountryId == countryId)
               .OrderBy(x => x.Name)
               .Select(x => new { x.Id, x.Name })
               .ToList();
  return Ok(list);
}

Quick tips and pitfalls: enable server-side caching per country to avoid repeated DB hits; add an index on the country key for fast lookups; for very large lists consider a "typeahead" UI instead of a huge dropdown; include a no-JS fallback (server-rendered select) for robustness; and use free datasets (GeoNames or similar) so you do not enter cities by hand. For debugging use the browser Network tab to inspect the JSON response and Content-Type.

Recommended Answers

All 2 Replies

You don't have to be lazy. Thats the best practice, Thats the demand of coding. You would need to create 2 tables, one for the city and another for the country. You obviously need to link up the 2 tables by having the primary key of country table as a forein key in the city table.

after that you want to bind your country drop down with the data from country and you also want to enable the autopostback property of your drop down.
you will then have to bind data on the city drop down in the index changed event of the country drop down. Makes sense?

awesome !!! i had kinda idea about that but you made it clear !!! thanks !!!
and sorry , i didn't mean to be lazy but i have a very shoert time to submit my project, (fianl year project) and i coded alot. so making shortcuts for things like this!!! but doesn't matter, as u said, i vl code it. Thanks !!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.