Building off Thomas Stock's answer, I created these overloaded ToSelectList
methods:
using System;
using System.Collections.Generic;
using System.Web.Mvc;
namespace Helpers {
public static class ControllerHelpers {
/// <summary>
/// Returns an IEnumerable<SelectListItem> by using the specified items for data value field.
/// </summary>
/// <param name="enumerable">The items.</param>
/// <param name="value">The data value field.</param>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, string> value) {
return enumerable.ToSelectList(value, value, null);
}
/// <summary>
/// Returns an IEnumerable<SelectListItem> by using the specified items for data value field and a selected value.
/// </summary>
/// <param name="enumerable">The items.</param>
/// <param name="value">The data value field.</param>
/// <param name="selectedValue">The selected value.</param>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, string> value, object selectedValue) {
return enumerable.ToSelectList(value, value, selectedValue);
}
/// <summary>
/// Returns an IEnumerable<SelectListItem> by using the specified items for data value field and the data text field.
/// </summary>
/// <param name="enumerable">The items.</param>
/// <param name="value">The data value field.</param>
/// <param name="text">The data text field.</param>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, string> value, Func<T, string> text) {
return enumerable.ToSelectList(value, text, null);
}
/// <summary>
/// Returns an IEnumerable<SelectListItem> by using the specified items for data value field, the data text field, and a selected value.
/// </summary>
/// <param name="enumerable">The items.</param>
/// <param name="value">The data value field.</param>
/// <param name="text">The data text field.</param>
/// <param name="selectedValue">The selected value.</param>
public static IEnumerable<SelectListItem> ToSelectList<T>(this IEnumerable<T> enumerable, Func<T, string> value, Func<T, string> text, object selectedValue) {
foreach (var f in enumerable) {
yield return new SelectListItem() {
Value = value(f),
Text = text(f),
Selected = value(f).Equals(selectedValue)
};
}
}
}
}
In your controller (using
the appropriate namespace from above), do the following:
var pageOptions = new[] { "10", "15", "25", "50", "100", "1000" };
ViewData["PageOptions"] = pageOptions.ToSelectList(o => o as string, "15" /*selectedValue*/);
And finally in your View, put:
<%= Html.DropDownList("PageOptionsDropDown", ViewData["PageOptions"] as IEnumerable<SelectListItem>, "(Select one)") %>
It will result in the desired output--of course, you can leave out the "(Select one)"
optionLabel above if you don't want the first empty item:
<select id="PageOptionsDropDown" name="PageOptionsDropDown">
<option value="">(Select one)</option>
<option value="10">10</option>
<option selected="selected" value="15">15</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<option value="1000">1000</option>
</select>
Update: A complete code listing can be found here with even more helpful overloads.