2015-05-26 83 views
0

查看:如何填充基於另一個下拉列表?

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

主要思想是,當我選擇的國家獲得國家,當我點擊狀態,以獲得城市.. 。國家recive國家的id,​​所以我需要用國家的id填充狀態來獲得國家,然後引用recive狀態的國家ID來填充城市

+0

你不能這樣做完全是在服務器上。你需要一些客戶端JavaScript。這可能會有所幫助:http://blogs.msdn.com/b/rickandy/archive/2012/01/09/cascasding-dropdownlist-in-asp-net-mvc.aspx –

回答

0

您需要一些JavaScript函數,它會觸發下拉更改事件,並將選定父項的相應子元素放入下拉菜單中。一旦您收到回覆,請填寫您擁有最新價值的子女下拉菜單。

簡單的例子:sample

相關問題