我無法確定爲什麼DropDownListFor沒有選擇正確的選項。下面是我的代碼,我有硬編碼的'選擇'值進行測試,但仍然在頁面加載時,我將'選擇國家'作爲選定值。下拉選定的項目在DropDownList中工作,但不在DropDownListFor中 - MVC
@Html.DropDownListFor(x => x.CountryID,
new List<SelectListItem> {
new SelectListItem { Text = "USA", Value = "US", Selected = false},
new SelectListItem { Text = "UK", Value = "UK", Selected = true},
new SelectListItem { Text = "Australia", Value = "AUS", Selected = false }
},"Select Country")
但是,對於DropDownList,同樣工作正常!
@Html.DropDownList("CountryID",
new List<SelectListItem> {
new SelectListItem { Text = "USA", Value = "US", Selected = false},
new SelectListItem { Text = "UK", Value = "UK", Selected = true},
new SelectListItem { Text = "Australia", Value = "AUS", Selected = false }
},"Select Country")
請幫忙。
你應該Model.CountryID的值設置爲你想要的項目被選中 – Carl 2014-10-30 10:29:46