c# - 基于国家选择动态改变MVC中的州/省
问题描述
所以,我知道那里已经有很多例子,但我已经尝试了其中的大部分,但似乎仍然无法让它为我工作。我想选择一个 Enum 的国家,然后使用 JSON 来切换省和州。现在,我可以根据我在视图中选择的列表加载一个或另一个。
模型
public string Province { get; set; }
public enum Countries
{
[Display(Name = "Canada")]
Canada,
[Display(Name = "United States")]
UnitedStates,
}
控制器
[HttpPost]
public JsonResult GetStatesOrProvinces(string id)
{
IEnumerable<SelectListItem> stateProvListEmpty = new List<SelectListItem>();
SelectList stateProvList = new SelectList(stateProvListEmpty);
switch (id)
{
case "Canada":
stateProvList = Lists.provinceList;
break;
case "United States":
stateProvList = Lists.stateList;
break;
}
return Json(stateProvList);
}
赫勒级
public class Lists
{
public static SelectList provinceList = new SelectList(
new List<SelectListItem>{
new SelectListItem { Text = "Alberta", Value = "Alberta" },
new SelectListItem { Text = "British Columbia", Value = "British Columbia" },
new SelectListItem { Text = "Manitoba", Value = "Manitoba"},
new SelectListItem { Text = "New Brunswick", Value = "New Brunswick"},
new SelectListItem { Text = "Newfoundland and Labrodor", Value = "Newfoundland and Labrodor" },
new SelectListItem { Text = "Nova Scotia", Value = "Nova Scotia" },
new SelectListItem { Text = "Ontario", Value = "Ontario" },
new SelectListItem { Text = "Prince Edward Island", Value = "Prince Edward Island" },
new SelectListItem { Text = "Quebec", Value = "Quebec" },
new SelectListItem { Text = "Saskatchewan", Value = "Saskatchewan" },
}, "Value", "Text");
public static SelectList stateList = new SelectList(
new List<SelectListItem>{
new SelectListItem() {Text="Alabama", Value="AL"},
new SelectListItem() { Text="Alaska", Value="AK"},
new SelectListItem() { Text="Wyoming", Value="WY"}
}, "Value", "Text");
}
看法
<div class="form-group">
@Html.LabelFor(model => model.Province, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Province, Lists.provinceList, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Province, "", new { @class = "text- danger" })
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.js%22%3E"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js%22%3E"></script>
<script type="text/javascript">
$(document).ready(function () {
//Dropdownlist Selectedchange event
$("#Country").change(function () {
$("#Province").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("GetStatesOrProvinces")', // we are calling json method
dataType: 'json',
data: { id: $("#Country").val() },
// here we are get value of selected country and passing same value
// as input to json method GetStatesOrProvinces.
success: function (stateProvList) {
// states contains the JSON formatted list
// of states passed from the controller
$.each(stateProvList, function (i, province) {
$("#Province").append('<option value="' + province.Value + '">' +
province.Text + '</option>');
// here we are adding option for States
});
},
error: function (ex) {
alert('Failed to retrieve States/Provinces.' + ex);
}
});
return false;
})
});
</script>
解决方案
推荐阅读
- html - 微软边缘浏览器中的材料 UI 可见性密码出现问题检查了不同的方法
- grep - 如何从两个列表中删除列表中的目录?
- terraform - 如何将地形变量转换为二头肌兼容的参数/变量
- sql - 最后一行的 SQL 组和条件总和
- ansible - Ansible:比较字典并创建一个带有结果的新字典
- sql - 春季批处理中不支持查询计数会引发异常错误
- android - CropImageActivity - 在 Redmi 6A (Android 8.1.0) 上点击 CROP(应用程序崩溃)并在 Galaxy M30 (Android 10) 上运行
- sql - COUNT 和 COUNT 的 ROLLUP SUM
- python - 找不到 Selenium 的按钮
- angular - Angular - 在客户刷新页面或清理缓存之前,不会在生产中显示更改