Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MarshalOfficial/66abb58c4b2cb28d4ca83f312653970f to your computer and use it in GitHub Desktop.
Save MarshalOfficial/66abb58c4b2cb28d4ca83f312653970f to your computer and use it in GitHub Desktop.
Countries and cities drop down manipulation in asp.net MVC project
  1. Create Razor Page: Create a new Razor Page named Index.cshtml or any other relevant name.

  2. Design the UI: In your Razor Page's HTML markup, create two dropdown lists - one for countries and one for cities:

    <select id="countryDropdown">
        <option value="">Select Country</option>
        <!-- Populate countries dynamically if needed -->
    </select>
    
    <select id="cityDropdown">
        <option value="">Select City</option>
        <!-- Cities will be populated dynamically based on selected country -->
    </select>
  3. Implement AJAX Call: Use jQuery to make an AJAX call to fetch cities based on the selected country:

    <script>
    $(document).ready(function() {
        $('#countryDropdown').change(function() {
            var selectedCountry = $(this).val();
            $.ajax({
                url: '/YourController/GetCities', // Endpoint to fetch cities
                method: 'GET',
                data: { country: selectedCountry },
                success: function(response) {
                    $('#cityDropdown').empty(); // Clear existing options
                    $.each(response, function(index, city) {
                        $('#cityDropdown').append($('<option>').text(city).attr('value', city));
                    });
                },
                error: function(xhr, status, error) {
                    console.error('Error:', error);
                }
            });
        });
    });
    </script>
  4. Create Controller Action: In your ASP.NET MVC application, create a controller action to handle the AJAX request and return cities based on the selected country:

    public class YourController : Controller
    {
        public IActionResult GetCities(string country)
        {
            // Implement logic to fetch cities based on the selected country
            List<string> cities = GetCitiesByCountry(country); // Dummy function, replace with your logic
            return Json(cities);
        }
    
        // Dummy function to return cities based on country (replace with actual logic)
        private List<string> GetCitiesByCountry(string country)
        {
            // Your logic to fetch cities based on country goes here
            // This is just a placeholder, replace it with actual data retrieval logic
            List<string> cities = new List<string>();
            if (country == "USA")
            {
                cities.Add("New York");
                cities.Add("Los Angeles");
                cities.Add("Chicago");
            }
            else if (country == "UK")
            {
                cities.Add("London");
                cities.Add("Manchester");
                cities.Add("Birmingham");
            }
            // Add more countries and cities as needed
            return cities;
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment