I am using Html.DropDownListFor to build a select list. It is a simple list of numbers from 1 to 100. One of the parameters – selectList As System.Collections.Generic.IEnumerable(Of SelectListItem) – is the list’s options, which I have usually built manually, like this:
@Html.DropDownListFor(Function(x) x.Sorting.IsAscending, _
New SelectList(New Dictionary(Of String, Boolean) From _
{{"Sort Ascending", True}, {"Sort Descending", False}}, "value", "key"))
Or from an enum, like this:
@Html.DropDownListFor(Function(x) x.Sorting.SortFieldCurrent, _
New SelectList(Model.Sorting.SortFields, "value", "key"))
But this time I want a list from 1 to 100. And I refuse to manually create it 🙂
Is there some sweet LINQ magic to build a list for me?
Use
Enumerable.Rangeto generate the range of numbers:C#:
VB (Courtesy http://www.developerfusion.com/tools/convert/csharp-to-vb/):
http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx