如何根据另一个填充下拉列表?

时间:2015-05-26 12:37:25

标签: asp.net-mvc

查看:

       <div class="form-group">
 <label for="country" class="control-label col-lg-5">@Html.Label(@BetXOnline.TranslationProvider.Instance.GetTranslationMessage("COUNTRY")):@Html.ValidationMessageFor(m => m.Register.SelectCountryId, null, new { @class = "required" })</label>
<div class="col-lg-7">
@Html.DropDownListFor(m => m.Register.SelectCountryId, Model.Register.Country, "Select country", new { id = "country", @class = "form-control" })
</div>
</div>

 <!-- Region-->
 <div class="form-group">
 <label for="region" class="control-label col-lg-5">@Html.Label(@BetXOnline.TranslationProvider.Instance.GetTranslationMessage("REGION")):@Html.ValidationMessageFor(m => m.Register.SelectRegionId, null, new { @class = "required" })</label>
<div class="col-lg-7">
 @Html.DropDownListFor(m => m.Register.SelectRegionId, Model.Register.Region, "Select region", new { id = "region", @class = "form-control" })
 </div>
  </div>

 <!-- City-->
 <div class="form-group">
 <label for="region" class="control-label col-lg-5">@Html.Label(@BetXOnline.TranslationProvider.Instance.GetTranslationMessage("CITY")):@Html.ValidationMessageFor(m => m.Register.SelectCityId, null, new { @class = "required" })</label>
<div class="col-lg-7">
@Html.DropDownListFor(m => m.Register.SelectCityId, Model.Register.City, "Select region", new { id = "region", @class = "form-control" })
 </div>
</div>

控制器:

 model.Register.Country = new SelectList(manager.GetCountries(), "Id", "Name");  
            model.Register.Region = new SelectList(manager.GetRegions(model.Register.SelectCountryId), "Id", "Name");
            model.Register.WebTypeOfLicense = new SelectList(CommonDbService.GetAllLicense(), "Id", "Name");
            model.Register.City = new SelectList(manager.GetCities(model.Register.SelectRegionId), "Id", "Name");

功能:

IEnumerable<ICountry> GetAllCountries();
        ICountry GetCountry(int countryId);
        IEnumerable<IRegion> GetRegions(int countryId);
        IRegion GetRegion(int regionId);
        IEnumerable<ICity> GetCities(int regionId);
        ICity GetCity(int cityId);

主要的想法是,当我选择国家获得国家,当我点击州获得城市时......国家重新获得国家的ID,所以我需要填充国家ID的国家来获得州,然后引用国家的ID填充城市

1 个答案:

答案 0 :(得分:0)

您需要一些javascript函数,它将在下拉列表更改事件中触发,并在下拉列表中显示所选父项的相应子元素。一旦收到回复,请填写具有最新价值的子下拉列表。

一个简单的例子:sample