开发者

ASP.NET MVC - how to set the dataValueField of a SelectList when binding to a List<string>

What do I use to set the dataValueField of the SelectList when I'm using a L开发者_开发百科ist<string>. I want to be able to set the value of the option in the select. If I have:

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

and I want my html to be:

<select>
  <option value="apple">apple</option>
  <option value="orange">orange</option>
</select>


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());
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜