我有一个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/)”,其中的共识是“不,他们不应该”。 –