Toby Caulk
Toby Caulk

Reputation: 276

Dropdownlist with unknown amount of values

I have a dropdownlistand I would like to bind my Dictionaryto it where the keys are the displayed items and the values are stored in the value attribute tag.

I found this: bind-html-dropdownlist-with-static-items

But it doesn't allow for an unknown number of items to be bound as you have to manually enter the SelectListItem. I tried this:

@Html.DropDownList("OverrideConfigList", new List<SelectListItem>
{
     for(KeyValuePair<string, string> entry in Model.IdentifiFIConfiguration.Config.Configuration)
     {
         new SelectListItem { Text = entry.Key, Value = entry.Value}
     }
})

But that didn't work either. Any suggestions?

Edit: My model class looks basically like this:

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 holds Config which looks like this:

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;
    }
}

Upvotes: 0

Views: 229

Answers (2)

DarkVision
DarkVision

Reputation: 1423

you cannot bind a dropdown list to a dictionnary you need scalar property to bind select value also you need a collection to bind a dropdownlist you could do this but that ugly

@Html.DropDownList("SelectedItemValue", new SelectList(MyDictionary, "Key", "Value"))

Upvotes: 0

David
David

Reputation: 218950

Your attempt is close, but the syntax is wrong. You can't execute a for loop in a list initializer like that.

Essentially, what you're trying to do is transform a collection of one thing (key/value pairs) to a collection of another thing (SelectListItems). You can do that with a LINQ select:

Model.IdentifiFIConfiguration.Config.Configuration.Select(c => new SelectListItem { Text = c.Key, Value = c.Value })

You may optionally need to add a .ToList() or .ToArray() at the end either for static typing or to materialize the collection sooner, but that wouldn't affect the logic of the statement.

This transformation would result in the list of SelectListItems that you want:

@Html.DropDownList(
    "OverrideConfigList",
    Model.IdentifiFIConfiguration.Config.Configuration.Select(c => new SelectListItem { Text = c.Key, Value = c.Value })
)

Upvotes: 2

Related Questions