tags:

views:

21

answers:

1

What do I use to set the dataValueField of the Select List when I'm using a List. I want to be able to set the value of the option in the select. If i have:

List list = new List(); list.Add("apple"); list.Add("orange");

and I want my html to be:

apple orange

A: 

You can't do this using the DataValueField. You can create your own class which wraps your list for your drop down:

public class ListWrapper
{
    private List<string> _list;

    public ListWrapper(List<string> list)
    {
        this._list = list;
    }

    public override string ToString()
    {
        return String.Join(" ", _list.ToArray());
    }
}
Yuriy Faktorovich
how would the Html.DropDownList syntax look like?
Justin S.
seems easier just to use a Dictionary<string,string> and use Key,Value to set the properties
Justin S.
I think I am misunderstanding the question, can you place the items in the drop down and the list coming in?
Yuriy Faktorovich