Reputation:
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?
Upvotes: 0
Views: 82
Reputation: 33381
Your GetSpacedOptions
has optional parameter zeroPad
with default value false
.
Use
var values = EnumUtilities.GetSpacedOptions<T>(true);
instead of
var values = EnumUtilities.GetSpacedOptions<T>();
Upvotes: 1