原文轉至http://www.cnblogs.com/rongstar/archive/2009/06/26/bindselect.html
在 MVC 中動態綁定下拉菜單的方法
1. 已知下拉菜單列表項:
在 Controller 控制器類中輸入已下代碼
1
public
class
DemoController : Controller
2 {
3 public ActionResult BindDropDownList()
4 {
5 List < SelectListItem > select1 = new List < SelectListItem >
6 {
7 new SelectListItem { Text = " 內容 " , Value = " 值 " },
8 new SelectListItem
9 };
10
11 ViewData[ " select1 " ] = new SelectList(select1, " Value " , " Text " , " 此處為默認項的值 " );
12
13 return View();
14 }
15 }
2 {
3 public ActionResult BindDropDownList()
4 {
5 List < SelectListItem > select1 = new List < SelectListItem >
6 {
7 new SelectListItem { Text = " 內容 " , Value = " 值 " },
8 new SelectListItem

9 };
10
11 ViewData[ " select1 " ] = new SelectList(select1, " Value " , " Text " , " 此處為默認項的值 " );
12
13 return View();
14 }
15 }
在 View 中使用
1
<%=
Html.DropDownList(
"
select1
"
)
%>
這種方法簡單明了,也比較方便,如果不用從數據庫中讀取數據的話,可以采用這種方法。
2. 從數據庫或者數組中循環讀取下拉列表項
此處省略數據庫連接代碼,從數據庫讀出的數據與字符串數組中存儲的數據類似,以下就以數組為例。
在 Controller 中控制器類中輸入已下代碼
1
public
class
DemoController : Controller
2 {
3 public ActionResult BindDropDownList()
4 {
5 string [] texts = new string [] { " 一 " , " 二 " , " 三 " , n
};
6 string [] values = new string [] { " 1 " , " 2 " , " 3 " , n
};
7
8 List < SelectListItem > select1 = new List < SelectListItem > ();
9
10 for ( int i = 0 ; i < texts.Length; i ++ )
11 {
12 select1.Add( new SelectListItem
13 {
14 Text = texts[i],
15 Value = values[i]
16 });
17 };
18
19 ViewData[ " select1 " ] = new SelectList(select1, " Value " , " Text " , " 此處為默認項的值 " );
20
21 return View();
22 }
23 }
2 {
3 public ActionResult BindDropDownList()
4 {
5 string [] texts = new string [] { " 一 " , " 二 " , " 三 " , n

6 string [] values = new string [] { " 1 " , " 2 " , " 3 " , n

7
8 List < SelectListItem > select1 = new List < SelectListItem > ();
9
10 for ( int i = 0 ; i < texts.Length; i ++ )
11 {
12 select1.Add( new SelectListItem
13 {
14 Text = texts[i],
15 Value = values[i]
16 });
17 };
18
19 ViewData[ " select1 " ] = new SelectList(select1, " Value " , " Text " , " 此處為默認項的值 " );
20
21 return View();
22 }
23 }
在 View 中使用
1
<%=
Html.DropDownList(
"
select1
"
)
%>
其實這種方法看起來跟第1種比較類似,只是讀取數據的時候,采用了一個循環的語句。
3. 從數據庫中讀取某表的所有下拉菜單列表項
此處假設已存在 Category 類,可以通過 Category.GetList() 方法獲取該表的所有分類,該表包含 ID 和 Name 兩個數據列。
在 Controller 中控制器類中輸入已下代碼
1
public
class
DemoController : Controller
2 {
3 public ActionResult BindDropDownList()
4 {
5 List < CategoryEntiry > categories = Category.GetAll();
6
7 ViewData[ " Categories " ] = new SelectList(categories, " ID " , " Name " );
8
9 return View();
10 }
11 }
2 {
3 public ActionResult BindDropDownList()
4 {
5 List < CategoryEntiry > categories = Category.GetAll();
6
7 ViewData[ " Categories " ] = new SelectList(categories, " ID " , " Name " );
8
9 return View();
10 }
11 }
在 View 中使用
1
//
首先將 ViewData 中的數據轉化為 SelectList
2 <% SelectList categories = ViewData[ " Categories " ] as SelectList; %>
3
4 // 然后 才能輸出
5 <%= Html.DropDownList( " Category " , categories) %>
2 <% SelectList categories = ViewData[ " Categories " ] as SelectList; %>
3
4 // 然后 才能輸出
5 <%= Html.DropDownList( " Category " , categories) %>
在這里需要注意,就是第3種與前2種在 View 中使用方法稍有不同,當然也可以將前2種方法改為第3種方法,或者將第3種方法改為前2種方法。