如何使用Dictionary<int,string>綁定DropDownList問題,今天既然查了,就記錄下來分享給大家。
@{
Dictionary<int, string> dictionaryTest = new Dictionary<int, string>()
{
{1, "Item n. 1"},
{2, "Item n. 2"},
{3, "Item n. 3"},
{4, "Item n. 4"},
};
SelectList selectListFromDictionary = new SelectList(
dictionaryTest.Select(x => new { value = x.Key, text = x.Value }),
"value",
"text"
);
}