Asp.net mvc ASP.NET MVC多重组合框

Asp.net mvc ASP.NET MVC多重组合框,asp.net-mvc,combobox,Asp.net Mvc,Combobox,我想有一个小的例子屏幕,应该有两个组合。第一个组合应显示国家表中的国家名称,在组合中选择国家名称后,下一个组合应显示地区名称 国家表结构: Country Name, Country Id 分区表结构 District id Country id District name 有人能帮我吗?有两种可能的方法来实现这一点 或者将所有组合放在html中,并在第一个组合中选择后使用javascript更改第二个组合的内容 或者在第一个组合上设置AutoPostback,并根据第一个组合的选定值在服务

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

国家表结构:

Country Name,
Country Id
分区表结构

District id
Country id
District name

有人能帮我吗?

有两种可能的方法来实现这一点

或者将所有组合放在html中,并在第一个组合中选择后使用javascript更改第二个组合的内容

或者在第一个组合上设置AutoPostback,并根据第一个组合的选定值在服务器端填充第二个组合。

这很容易

第一个下拉列表很简单,只需在模型中传递
IEnumerable
,就可以了

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

您只需调用一个方法并发送第一个下拉列表的值,然后在您的方法中,只需调用DB并返回一个
JsonResult

例如:


asp.net中没有“自动回发”,MVCI没有说有。您只需要使用javascript自己完成,这就是为什么我说“设置自动回邮”而不是“将自动回邮属性设置为true”
<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>
public ActionResult getDistricts(string country)
{
    List<Districts> districts = dbRepository.GetDistrictsByCountryId(country);

    return Json(districts, JsonRequestBehavior.AllowGet);
}