DropDownListFor
是可以使用的方法:選中此瞭解更多詳情:http://msdn.microsoft.com/en-us/library/system.web.mvc.html.selectextensions.dropdownlistfor.aspx
我會建議你創建
<select id="PeopleClass" name="PeopleClass">
<option value="1">Name1</option>
<option value="2">Name2</option>
<option value="3">Name3</option>
</select>
成IEnumerable<SelectListItem>
,並將其綁定當您使用數據庫中選定的值呈現視圖時。
// create People class
public class People{
public value {get; set}
public name {get; set}
}
//Create option list for your dropdown
List<People> peopleList=
new List<People>{ new People{ value="1", name ="Name 1"}, new People{ value="2", name ="Name 2"}, new People{ value="3", name ="Name 3"}};
//bind it with ViewData
ViewData["ddl"] = new SelectList(peopleList, "value", "name", valueFromDatabase);
在你看來
最後,下拉列表與ViewData["ddl"]
<%=Html.DropDownListFor(model => model.People,(IEnumerable<SelectListItem>)ViewData["ddl"])%>
我認爲這是一個MVC問題,而不是純html問題 – 2010-09-22 05:09:24
,但它的allways showitn name3如果我從databse獲得name2或Name1? – kumar 2010-09-22 05:26:32