I am binding a List to a DropDownList. But I want to give other values to the value per list item.
I have following ddl and list:
List<string>
sport, volleyball, rugby
<select>
<option selected="selected" value="0"> Alle soorten</option>
<option value="sport">sport</option>
<option value="volleyball">volleyball</option>
<option value="rugby">rugby</option>
</select>
But I want following (ID in the value)
<select>
<option selected="selected" value="0"> Alle soorten</option>
<option value="1">sport</option>
<option value="2">volleyball</option>
<option value="3">rugby</option>
</select>
How do I need to create a List so I can get the above dropdownlist.
Thanks
Here is the one more way. Convert the DataSet Table to list and bind to dropdown.
If you are able to change the type of your source I would recommend to use a dictionary. You can do it this way:
This would lead to this:
Later you can access the ID or value like this:
What you could do is to create the ListItems manually like this:
Or you could create a temporary variable and bind your drop down list with a linq query, like this:
and combine that with the markup: