it's kinda easy...
the 1st dropdown is easy, just pass the IEnumerable in the model and voilá.
the 2nd dropdown is as easy but just takes a little bit more code:
all you need to do is to call a method and send the value of the first dropdown, then in your method, just call the DB and return a JsonResult
example:
<select id="dropdown1">
<option value="" selected="true">Select country</option>
<% foreach(var country in Model.Countries) { %>
<option value="<%= country.Id %>"><%= country.Name %></option>
<% } %>
</select><br/>
<select id="dropdown2"></select>
at the end of the page
<script>
$(document).ready( function() {
$("#dropdown1").bind("change", function() {
// everytime the value of the dropdown 1 is changed, do this:
var countryId = $("#dropdown1").val();
$.get("/country/getDistricts", { 'country' : countryId }, function(data) {
$("#dropdown2").empty(); // clear old values if exist
var options = "";
for(i = 0; i < data.length; i++) { // build options
options += ("<option value='" + data[i].districtId + "'>" + data[i].districtName + "</option>");
}
$("#dropdown2").append(options);
});
});
});
</script>
in your Action at country Controller
public ActionResult getDistricts(string country)
{
List<Districts> districts = dbRepository.GetDistrictsByCountryId(country);
return Json(districts, JsonRequestBehavior.AllowGet);
}