I am using the following:
public static SelectList GetOptions<T>(string value = null) where T : struct
{
var values = EnumUtilities.GetSpacedOptions<T>();
var options = new SelectList(values, "Value", "Text", value);
return options;
}
public static IEnumerable<SelectListItem> GetSpacedOptions<T>(bool zeroPad = false) where T : struct
{
var t = typeof(T);
if (!t.IsEnum)
{
throw new ArgumentException("Not an enum type");
}
var numberFormat = zeroPad ? "D2" : "g";
var options = Enum.GetValues(t).Cast<T>()
.Select(x => new SelectListItem
{
Value = ((int) Enum.ToObject(t, x)).ToString(numberFormat),
Text = Regex.Replace(x.ToString(), "([A-Z])", " $1").Trim()
});
return options;
My enum has values:
public enum DefaultStatus {
Release = 0,
Review = 1,
InProgress = 2,
Concept = 3,
None = 99
};
From what I understand the number format should give my values of “01”,”02″ etc but it’s giving me “”1″,”2″,”3” ..
Is there something obvious I’m doing wrong?
Your
GetSpacedOptionshas optional parameterzeroPadwith default valuefalse.Use
instead of