我有一個dropdownlist
,我想將我的Dictionary
綁定到它,其中鍵是顯示的項目,值存儲在value
屬性標記中。未知數量值的下拉列表
我發現這一點: bind-html-dropdownlist-with-static-items
但它不允許未知數量的項目被綁定,你必須手動輸入SelectListItem
。我試過這個:
@Html.DropDownList("OverrideConfigList", new List<SelectListItem>
{
for(KeyValuePair<string, string> entry in Model.IdentifiFIConfiguration.Config.Configuration)
{
new SelectListItem { Text = entry.Key, Value = entry.Value}
}
})
但是那也沒用。有什麼建議麼?
編輯: 我的模型類基本上是這樣的:
public class DefaultConfigurationModel
{
public IdentifiFIConfiguration IdentifiFIConfiguration { get; set; }
public String FiKeySelection { get; set; }
public List<String> FiConfigKeys
{
get
{
if (IdentifiFIConfiguration.Config == null)
{
return null;
}
List<string> fiConfigKeys = new List<string>();
foreach (KeyValuePair<string, string> entry in IdentifiFIConfiguration.Config.Configuration)
{
fiConfigKeys.Add(entry.Key);
}
return fiConfigKeys;
}
}
}
IdentifiFIConfiguration
持有Config
看起來像這樣:
public class IdentifiConfiguration
{
public Dictionary<String, String> Configuration { get; set; }
public static IdentifiConfiguration DeserializeMapFromXML(string xml)
{
Dictionary<string, string> config = new Dictionary<string, string>();
XmlDocument configDoc = new XmlDocument();
configDoc.LoadXml(xml);
foreach (XmlNode node in configDoc.SelectNodes("/xml/*"))
{
config[node.Name] = node.InnerText;
}
IdentifiConfiguration identifiConfiguration = new IdentifiConfiguration()
{
Configuration = config
};
return identifiConfiguration;
}
}
模型類是什麼樣子? –
@FahadJameel編輯模特 –
我編輯了您的標題。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –