1
我有一個下拉列表,將不會正確顯示選定的值。它確實具有正確的元素,使用正確的文本和值,但似乎沒有辦法使其實際選擇正確的值。DropdownListFor不會選擇(誰會猜對?)
我所得到的是:
public class StateViewModel {
public string Name { get; set; }
public string Abbr { get; set; }
}
public class ChangeAddressViewModel {
//edited down to what's needed
public StateViewModel StateViewModel { get; set; }
public IEnumerable<StateViewModel> StateViewModels { get; set; }
}
我已經試過什麼:
@Html.DropDownListFor(model => model.StateViewModel
, Model.StateViewModels.Select(x => new SelectListItem {
Text = x.Name,
Value = x.Abbr,
Selected = x.Abbr == Model.StateViewModel.Abbr
})
, @Resource.SelectState)
//i've stepped through the lambda above and the expression
//x.Abbr == Model.StateViewModel.Abbr
//did evaluate to true for one of the iterations
@Html.DropDownListFor(model => model.StateViewModel
, new SelectList(Model.StateViewModels
, "Abbr"
, "Name"
, Model.StateViewModel.Abbr)
, @Resource.SelectState)
在您的視圖頁面上,您的模型定義在頁面頂部? – WEFX 2011-05-17 19:34:50
yup ...我現在正在工作...我將Model.StateViewModel更改爲一個字符串,它保存選定的值而不是選定的對象,出於某種原因,一切都很開心! ? – Mike 2011-05-20 17:02:00