Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I am going to describe it in ASP.NET MVC, but the same can be achieved if you either write an ASP.NET web service or just put a few page methods in your code behind to do the same - you'll also need a JSON serializer, either a 3rd party solution or the one in WCF.</p> <p>Using MVC, first, let's have three controller actions - one to display the page, countries will be static, and two to get the states and metros respectively:</p> <pre><code>public ActionResult Index() { ViewData["Countries"] = _countryRepository.GetList(); return View(); } public ActionResult States(string countryCode) { var states = _stateRepository.GetList(countryCode); return Json(states); } public ActionResult Metros(string countryCode, string state) { var metros = _metroRepository.GetList(countryCode, state); return Json(metros); } </code></pre> <p>In the view, you have three DropDownLists, one is bound to the ViewData["Countries"] object, say it's named Countries, you can get the states in jQuery with an Ajax call like this:</p> <pre><code>$('#Countries').change(function() { var val = $(this).val(); $states = $('#States'); $.ajax({ url: '&lt;%= Url.Action('States') %&gt;', dataType: 'json', data: { countryCode: val }, success: function(states) { $.each(states, function(i, state) { $states.append('&lt;option value="' + state.Abbr+ '"&gt;' + state.Name + '&lt;/option&gt;'); }); }, error: function() { alert('Failed to retrieve states.'); } }); }); </code></pre> <p>The Metros drop down would be filled analogically, passing both the country and state selection to the server and getting back a JSON object with an array of metro areas.</p> <p>I left out the details of repository implementation, just fill up the result variable on the server with a collection of states/metro areas in some way. I also made an assumption that the State class would have two properties - Abbr (e.g., 'CA') and Name (e.g., California).</p> <p>I hope that it helps you in any way, or at least directs you somehow towards the solution.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload