Shai Cohen
Shai Cohen

Reputation: 6249

HTML.DropDownListFor - Syntactic Sugar for SelectList

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?

Upvotes: 3

Views: 1088

Answers (1)

Kyle Trauberman
Kyle Trauberman

Reputation: 25694

Use Enumerable.Range to generate the range of numbers:

C#:

IEnumerable<int> range = Enumerable.Range(1, 100);

VB (Courtesy http://www.developerfusion.com/tools/convert/csharp-to-vb/):

Dim range As IEnumerable(Of Integer) = Enumerable.Range(1, 100)

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.range.aspx

Upvotes: 3

Related Questions