2012-03-12 2 views
2
var dict = new Dictionary<string,string>(); 
dict.Add("A","123"); 
dict.Add("B","456"); 
dict.Add("C","789"); 
dict.Add("D","000"); 
var list = new List<Dictionary<string,string>>(); //pretty much like a DataTable 
list.Add(dict); //more than one dict in the list 

//ddl is a dropdownlist 
ddl.DataSource = list 
ddl.DataTextField ="[A]"; 
ddl.DataValueField ="[C]"; 
ddl.DataBind(); 

WPF의 경우 위의 비슷한 바인딩을 수행 할 수 있지만 asp.net은 수행 할 수 없습니다.바인드 목록 <Dictionary <string, string >> to DropDownList

+0

잘 당신은 WPFs 바인딩 크기의 몇 ASP.NETs보다는 ** ** 더 나은 것을 알고? DataSource로 바인딩하기 전에 데이터를 변환해야합니다. - 죄송합니다. – Carsten

+0

문자열 사전을 asp.net의 드롭 다운 목록에 바인딩하고 싶습니다. 그렇습니까? – JayOnDotNet

답변

6

목록이 아닌 사전을 바인딩해야합니다. 이런 식으로 뭔가 :

var dict = new Dictionary<string,string>(); 
dict.Add("A","123"); 
... 

ddl.DataSource = dict 
ddl.DataTextField ="Key"; 
ddl.DataValueField ="Value"; 
ddl.DataBind(); 
3

이 나를 위해 잘 작동합니다 :

Dictionary<string, string> myDict = Dictionary<string, string>(); 
myDict.Add("myKey","My test value"); 

@Html.DropDownList("SomeDropDown", new SelectList(myDict, "key", "Value"), 
        "--- Select tomething ---", new { @class = "myHtmlClassName" }) 
관련 문제