foreach (Person person in personList) {
SelectListItem item = new SelectListItem();
item.Value = person.Id;
item.Text = person.FirstName + " " + person.LastName;
items.Add(item);
}
ViewData["personSelectList"] = new SelectList(items, "Value", "Text", 4);
<%=Html.DropDownList("personId", ViewData["personSelectList"] as SelectList)%>
此代碼未將Id = 4的人設置爲選定項目,而是始終選取列表中的第一個項目作爲選定項目。SelectList未在選擇項目中傳遞選定值
我錯過了什麼步驟?
可能重複http://stackoverflow.com/questions/624828/asp-net-mvc-html-dropdownlist-selectedvalue) – 2012-08-09 14:23:48