我有一個需要州和國家選擇的註冊頁面。填充這些下拉列表的項目來自外部數據庫。如何爲DropdownList加載列表
如何在頁面呈現之前調用這些列表來填充這些列表?
public class RegisterModel
{
...
public IEnumerable<SelectListItem> States {get;set;}
public IEnumerable<SelectListItem> Countries {get;set;}
...
}
//Register.cshtml
@model Adw.Web.Models.RegisterModel
@Html.LabelFor(m => m.State)
@Html.DropDownListFor(m =>m.State, new SelectList(Model.States))
//Controller
public ActionResult Register()
{
.....
RegisterModel rm = new RegisterModel();
//The factories return List<string>
rm.States = new SelectList(stateFactory.Create(states.Payload));
rm.Countries = new SelectList(countryFactory.Create(country.Payload));
return View(rm);
}
通過以上的設置,我收到:
有型「的IEnumerable」具有關鍵的「國」無ViewData的項目。
總結 - 我需要做一個web服務調用,以在頁面呈現之前獲取2個下拉列表的數據。
我最近回答了這樣的問題,也許那裏的信息會幫助你。 http://stackoverflow.com/questions/14714923/set-a-default-value-for-a-dropdownlist-from-a-list-coming-from-another-controlle/14715206#14715206 – mmeasor 2013-02-20 21:56:57
我試圖設置它正如您的鏈接中所建議的那樣,但仍似乎無法使其發揮作用。我已更新我的帖子以反映這些更改。 – Zholen 2013-02-21 05:27:20