2009-10-07 66 views

回答

1

好吧,让我来试试

还有就是你可以用它来创建控制器类列表(在选择恰当的控制器动作)如下的SelectList类:

var items = new KeyValueList(); 
var item = new KeyValue() {Key = 1, Value = "Orange" };  
items.Add(item); 
item = new KeyValue() {Key = 2, Value = "Apple" };  
items.Add(item); 

var myList = new SelectList(items, "Key", "Value", selectedItemId); 

的selectedItemId将是项目关键的价值。然后将myList添加到ViewData集合中,并使用一个可用于从视图中引用它的键。像:

ViewData["FruitList"] = myList; 

在视图,然后你可以使用:

  <p> 
       <label for="FruitList">Fruits:</label> 
       <%= Html.DropDownList("FruitList") %> 
      </p> 

在回发到控制器动作,“键”为选择的值作为的FormCollection或交的部分参数发送,并且您可以使用“FruitList”访问该值。

+0

什么名字空间包含KeyValueList? – 2010-03-01 02:56:14