gpt4 book ai didi

asp.net-mvc - ASP.NET MVC 多个组合框

转载 作者:行者123 更新时间:2023-12-04 06:35:46 25 4
gpt4 key购买 nike

我想要一个应该有两个组合的小示例屏幕。第一个应显示国家/地区表中的国家/地区名称,在组合中选择国家/地区名称后,下一个组合应显示地区名称。

国家表结构:

Country Name,
Country Id

区表结构。
District id
Country id
District name

任何人都可以帮助我吗?

最佳答案

这有点容易...

第一个下拉菜单很简单 ,只需通过IEnumerable在模型中,瞧。

第二个下拉菜单同样简单 但只需要多一点代码:

您需要做的就是调用一个方法并发送第一个下拉列表的值,然后在您的方法中,只需调用数据库并返回 JsonResult
例子:

<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>

在页面的末尾
<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>

在您的操作中 country Controller
public ActionResult getDistricts(string country)
{
List<Districts> districts = dbRepository.GetDistrictsByCountryId(country);

return Json(districts, JsonRequestBehavior.AllowGet);
}

关于asp.net-mvc - ASP.NET MVC 多个组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4897593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com