0

我正在使用以下内容:

    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;

我的枚举具有值:

public enum DefaultStatus {
    Release = 0,
    Review = 1,
    InProgress = 2,
    Concept = 3,
    None = 99
};

据我了解,数字格式应该给出我的值“01”、“02”等,但它给了我“1”、“2”、“3”..

有什么明显的我做错了吗?

4

1 回答 1

1

您有带有默认值的GetSpacedOptions可选参数。zeroPadfalse

利用

var values = EnumUtilities.GetSpacedOptions<T>(true);

代替

var values = EnumUtilities.GetSpacedOptions<T>();
于 2013-02-08T13:23:15.447 回答