一:Html.DropDownList 数据绑定并选定默认项
1.建立IEnumerable<SelectListItem>并在其中指定默认选中项。
List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Kirin", Value = "29"
});
items.Add(new SelectListItem { Text = "Jade", Value = "28",
Selected = true});
items.Add(new SelectListItem { Text = "Yao", Value =
"24"});
this.ViewData["list"] = items;
然后在View中<%=Html.DropDownList("list")%>即可绑定数据,且选中第二项。
2.建立IEnumerable<SelectListItem>,在单独的ViewData项或view model的属性中指定默认选中项。
使用SelectList。
List<SelectListItem> items = new
List<SelectListItem>();
items.Add(new SelectListItem { Text = "Kirin", Value = "29"
});
items.Add(new SelectListItem { Text = "Jade", Value =
"28"});
items.Add(new SelectListItem { Text = "Yao", Value =
"24"});
this.ViewData["list"] = items;
this.ViewData["selected"] = 24;
3.使用SelectList。
var users = GetUsers();
var selectList = new SelectList(users, "Age", "Name",
"24");
this.ViewData["list"] = selectList;
<%=Html.DropDownList("list")%>
4.
<%=Html.DropDownList("ddlTest",ViewData["ddlTestData"]
摘自 http://blog.sina.com.cn/s/blog_69cfe5430100o2me.html