Sameera
Sameera

Reputation: 429

Dictionary Keys and Values to a Select List

Dictionary<string,string> dict = new Dictionary<string,string>();    
  dict.add("a1", "Car");
  dict.add("a2", "Van");
  dict.add("a3", "Bus");

SelectList SelectList = new SelectList((IEnumerable)mylist, "ID", "Name", selectedValue);

In above code I have put a list mylist to a SelectList. ID and Name are two properties of that particular object list(mylist).

Likewise I need to add the Dictionary to the SelectList.


Need to add the key of the dictionary to the data Value parameter -(ID position of above example) Need to add the value of the dictionary to the data text parameter -(Name position of the above example)

So please tell me a way to create a select list using this dictionary keys and values without creating a new class.

Upvotes: 26

Views: 45740

Answers (6)

Vali.Pay
Vali.Pay

Reputation: 384

I used this for a list of SelectListItem and it works fine for select tag and dropdown

dict.OrderBy(x => x.Value).Select(r => new SelectListItem(r.Key, r.Value));

Upvotes: 1

Nick N.
Nick N.

Reputation: 13558

I like to write it like this:

@Html.DropDownListFor(model => model.Delimiter,
                                         new Dictionary<string, string>
                                         {
                                             {",", ", (Comma)"},
                                             { ";", "; (Semicolon)"}
                                         }.Select(x => new SelectListItem {Value = x.Key, Text = x.Value}))

Because this way, you don't have to rely on the strings "Key" and "Value".

Upvotes: 3

Kneemin
Kneemin

Reputation: 868

All you really need to do is pass the dictionary as a parameter and use the overload:

public SelectList(IEnumerable items, string dataValueField, string dataTextField);

Example:

var dictionary = new Dictionary<string, string>
{
   {"a1", "Car"}, 
   {"a2", "Van"}, 
   {"a3", "Bus"}
};

var selectList = new SelectList(dictionary, "Key", "Value");

I know the post is a bit old but I came here to find the answer and came to this conclusion based on the previously given answers.

Upvotes: 27

James Lawler
James Lawler

Reputation: 56

You could construct a list of SelectListItem objects from the Dictionary and then create a SelectList from that.

var dict = new Dictionary<string, string>
{
   {"a1", "Car"}, 
   {"a2", "Van"}, 
   {"a3", "Bus"}
};

var myListItems = new List<SelectListItem>();

myListItems.AddRange(dict.Select(keyValuePair => new SelectListItem()
{
    Value = keyValuePair.Key, 
    Text = keyValuePair.Value
}));

var myList = new SelectList(myListItems);

Upvotes: 3

Nagaraj S
Nagaraj S

Reputation: 13484

Try

SelectList SelectList = new SelectList((IEnumerable)mylist, "Key", "Value", selectedValue);

Upvotes: 0

to StackOverflow
to StackOverflow

Reputation: 124794

You could try:

SelectList SelectList = new SelectList((IEnumerable)dict, "Key", "Value", selectedValue);

Dictionary<string, string> implements IEnumerable<KeyValuePair<string, string>>, and KeyValuePair gives you the Key and Value properties.

Be aware, however, that the order of items returned by enumerating a Dictionary<string,string> is not guaranteed. If you want a guaranteed order, you can do something like:

SelectList SelectList = new SelectList(dict.OrderBy(x => x.Value), "Key", "Value", selectedValue);

Upvotes: 45

Related Questions