c# - 如何创建国家/地区下拉栏
问题描述
我正在创建一个项目,我需要在其中创建一个带有条件国家/地区下拉栏的注册菜单。我只是不知道该怎么做。
我试过查找其他的东西,但找不到怎么做。我在 A MVC 工作,只知道基础知识。
我想这样做,所以首先需要一个国家下拉列表并更新状态下拉列表而不重新加载页面
解决方案
大部分代码来自 https://www.c-sharpcorner.com/blogs/cascading-dropdownlist-in-asp-net-mvc 但有很多更改可以转换为您正在寻找的内容
在视图中将其用于您的国家和州下拉列表
<div class="form-group">
@Html.LabelFor(model => model.CountryStr, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.CountryStr, ViewBag.Country as List<SelectListItem>, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StateStr, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.StateStr, new SelectList(string.Empty, "Value", "Text"), "--Select State--", new { @class = "form-control" })
</div>
</div>
这在它下面
<script src="~/Scripts/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#CountryStr").change(function () {
$("#StateStr").empty();
$.ajax({
type: 'POST',
url: '@Url.Action("getState")',
dataType: 'json',
data: { id: $("#CountryStr").val() },
success: function (State) {
$.each(State, function (i, State) {
$("#StateStr").append('<option value="'
+ State.Value + '">'
+ State.Text + '</option>');
});
},
error: function (ex) {
alert('Failed.' + ex);
}
});
return false;
})
});
</script>
将其添加到顶部以加载国家/地区列表(使用 @{ }):
string[] whwhw = ListofCountries
IEnumerable<string>
why = whwhw;
SelectList CountryLit = new SelectList(why);
并将其放入您的控制器中
public void bindCountry()
{
List<SelectListItem> li = new List<SelectListItem>();
li.Add(new SelectListItem { Text = "--Select Country--", Value = "0" });
cs.Open();
SqlDataReader dr = (da.SelectCommand = new SqlCommand("SELECT * FROM Country", cs)).ExecuteReader();
while (dr.Read())
{
li.Add(new SelectListItem { Text = dr.GetValue(1).ToString(), Value = dr.GetValue(0).ToString()});
ViewBag.Country = li;
}
cs.Close();
}
public JsonResult getState(int id)
{
List<SelectListItem> liStates = new List<SelectListItem>();
liStates.Add(new SelectListItem { Text = "--Select State--", Value = "0" });
cs.Open();
SqlDataReader dr = (da.SelectCommand = new SqlCommand("SELECT * FROM State WHERE CountryId = " + id, cs)).ExecuteReader();
int count = 0;
while (dr.Read())
{
liStates.Add(new SelectListItem { Text = dr.GetValue(1).ToString(), Value = dr.GetValue(0).ToString() });
ViewBag.State = liStates;
count += 1;
}
cs.Close();
PersonValues.Country = count;
return Json(new SelectList(liStates, "Value", "Text", JsonRequestBehavior.AllowGet));
}
并放
bindCountry();
在您返回页面视图之前,下拉栏将位于
推荐阅读
- r - 如何更改依赖于另一个二进制变量的二进制变量?
- postgresql - 将新列添加到 Postgres 物化视图
- reactjs - 在 React 中更改多个键值和 setState
- python - 如何让 discord.py 等待
- algorithm - 如何为这个类似“球和盒子”的背包问题创建一个贪心算法?
- python - 转换为浮点数以获取前 10 个最大值时出错
- python - 我怎样才能加快这段代码?应用和 for 循环确实减慢了速度
- android - 在 appcenter 中,单元测试失败仅指定函数名称和行号
- c++ - 带有结构的链表的奇怪行为
- git - git merge master 没有将 master 的更改/冲突合并到本地分支