2013-09-22 62 views
0

我有需要綁定到mvc下拉列表的字典列表。在mvc中綁定字典列表到下拉列表

public static readonly Dictionary<string, string> lstGender = new Dictionary<string, string>() { { "M", "Male" }, { "F", "Female" } }; 

如何將lstGender綁定到mvc中的dropdownlist。

預先感謝

+0

可能有[Bindi ng下拉列表中的mvc3到字典?](http://stackoverflow.com/questions/9983895/binding-a-dropdownlist-in-mvc3-to-a-dictionary) –

回答

0

做這樣的..

Dictionary<Int32, string> dict = new Dictionary<int, string>(); 
      dict.Add(0, "All"); 
      dict.Add(1, "Male"); 
      dict.Add(2, "Female"); 
      dict.Add(3, "Snr Citizen"); 
      dict.Add(4, "Children"); 


      ViewBag.Buckets = dict.Select(item => new SelectListItem() { Value = item.Key.ToString(), Text = item.Value }); 

鑑於這樣

@ Html.DropDownList( 「ddlbucket」,(IEnumerable的)ViewBag.Buckets)結合