希望能够轻松回答。我使用下面的代码生成级联下拉列表,但是,当我选择国家时,州自动填充我的州表中的第一个值(适用于相应的国家/地区)。我认为我需要在某处添加一个空字符串,但我不确定在哪里。级联下拉需要空白值MVC5
感谢
h2>Cascading DropDownList Sample</h2>
<div>
<div>
@Html.DropDownList("dropdownCountry", new SelectList(string.Empty, "Value", "Text"), "Please select a country", new { @style = "width:250px;" })
</div>
<div style="margin-top:50px;">
@Html.DropDownList("dropdownState", new SelectList(string.Empty, "Value", "Text"), "Please select a state", new { @style = "width:250px;" })
</div>
</div>
@section scripts
{
<script>
$(function() {
$.ajax({
type: "GET",
url: "/test/getcountries",
datatype: "Json",
success: function (data) {
$.each(data, function (index, value) {
$('#dropdownCountry').append('<option value="' + value.CountryId + '">' + value.CountryName +'</option>');
});
}
});
$('#dropdownCountry').change(function() {
$('#dropdownState').empty();
$.ajax({
type: "POST",
url: "/test/GetStatesByCountryId",
datatype: "Json",
data: { countryId: $('#dropdownCountry').val() },
success: function (data) {
$.each(data, function (index, value) {
$('#dropdownState').append('<option value ="' + value.Id + '">' + value.StateName + '</option>');
});
}
});
});
});
</script>
}
建议你参考[这个答案](https://stackoverflow.com/questions/33247717/how-to-keep-cascade-dropdownlist-selected-items-after-form-submit/33248183#33248183)为正确处理级联下拉列表的方式 –