正如你提到的:城镇是动态的,并且能够从数据库中读取您可以通过ajax传递来获取动态数据。
Ajax 从控制器获取读书来自基于数据库的国家/地区哪个用户选择了:
<script>
$(document).ready(function () {
$("#counties").on('change', function () {
var selectedItem = $(this).val();
var ddlStates = $("#towns"); // will be update after success ajax call
var statesProgress = $("#states-loading-progress");
statesProgress.show();
$.ajax({ // get states/towns from db from controller
cache: false,
type: "GET",
url: "@(Url.RouteUrl("GetStatesByCountryId"))",
data: { "countryId": selectedItem, "addSelectStateItem": "true" },
success: function (data) {
ddlStates.html('');
$.each(data, function (id, option) {
ddlStates.append($('<option></option>').val(option.id).html(option.name));
}); // populating result
statesProgress.hide(); // hide loader
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Failed to retrieve states.');
statesProgress.hide();
}
});
});
});
</script>
在控制器上:
public virtual IActionResult GetStatesByCountryId(string countryId, bool addSelectStateItem)
{
var model = _countryModelFactory.GetStatesByCountryId(countryId, addSelectStateItem);
return Json(model);
}
Then on 数据访问层 _countryModelFactory.GetStatesByCountryId()
i get 城镇/州来自基于国家/城镇用户选择的数据库。
Update :这是我从数据库(动态国家/城镇)检索州/城镇并将其填充到的方式selectbox
在我的代码中。