Ejemplo n.º 1
0
 internal void AddFilter(IEnumFilter filter)
 {
     _filters.Add(filter);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// 生成普通枚举类型的枚举值列表
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <param name="enumType"></param>
        /// <param name="filter"></param>
        /// <param name="enumValue"></param>
        /// <param name="optionLabel"></param>
        /// <param name="includeZero"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        private static MvcHtmlString StandardEnumValueList(HtmlHelper htmlHelper, string name,
            Type enumType, IEnumFilter filter, int enumValue, string optionLabel, bool includeZero, object htmlAttributes)
        {
            IList<SelectListItem> items = new List<SelectListItem>();
            foreach (FieldInfo fieldInfo in enumType.GetFields(BindingFlags.Public | BindingFlags.Static))
            {
                // 筛选
                if (filter != null)
                {
                    if (!filter.Test(fieldInfo))
                    {
                        continue;
                    }
                }

                object value = fieldInfo.GetValue(null);
                int intValue = Convert.ToInt32(value);

                if (!includeZero && intValue == 0)
                {
                    continue;
                }

                object[] attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), true);
                if (attributes.Length == 0)
                {
                    items.Add(new SelectListItem()
                    {
                        Value = intValue.ToString(),
                        Text = value.ToString(),
                        Selected = (enumValue == intValue),
                    });
                }
                else
                {
                    DescriptionAttribute description = attributes[0] as DescriptionAttribute;
                    items.Add(new SelectListItem()
                    {
                        Value = intValue.ToString(),
                        Text = description.Description,
                        Selected = (enumValue == intValue),
                    });
                }
            }

            object currentValue = htmlHelper.ViewData.Eval(name);
            htmlHelper.ViewData[name] = enumValue;
            MvcHtmlString result = htmlHelper.DropDownList(name, items, optionLabel, htmlAttributes);
            htmlHelper.ViewData[name] = currentValue;
            return result;
        }
Ejemplo n.º 3
0
 /// <summary>
 /// Define a custom filter.  It is recommended to use the <c>Custom</c> EXTENSION method
 /// instead, for a simple LINQ-based filter.
 /// </summary>
 public EnumGenie Custom(IEnumFilter filter)
 {
     _enumGenie.AddFilter(filter);
     return(_enumGenie);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 根据 enumType 的指示绘制以 value 为默认值的下拉框。
 /// name 只用于控件标识及名称
 /// </summary>
 /// <param name="htmlHelper"></param>
 /// <param name="name"></param>
 /// <param name="enumType"></param>
 /// <param name="filter"></param>
 /// <param name="value"></param>
 /// <param name="optionLabel"></param>
 /// <param name="htmlAttributes"></param>
 /// <returns></returns>
 public static MvcHtmlString EnumValueList(this HtmlHelper htmlHelper, string name,
     Type enumType, IEnumFilter filter, object value, string optionLabel, object htmlAttributes)
 {
     return EnumValueList(htmlHelper, name, enumType, filter, value, optionLabel, string.IsNullOrEmpty(optionLabel), htmlAttributes);
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 根据 enumType 的指示绘制以 value 为默认值的下拉框。
 /// name 只用于控件标识及名称
 /// </summary>
 /// <param name="htmlHelper"></param>
 /// <param name="name"></param>
 /// <param name="enumType"></param>
 /// <param name="filter"></param>
 /// <param name="value"></param>
 /// <param name="optionLabel"></param>
 /// <param name="includeZero"></param>
 /// <param name="htmlAttributes"></param>
 /// <returns></returns>
 public static MvcHtmlString EnumValueList(this HtmlHelper htmlHelper, string name,
     Type enumType, IEnumFilter filter, object value, string optionLabel, bool includeZero, object htmlAttributes)
 {
     int enumValue = int.MinValue;
     if (value != null)
     {
         if (value is string)
         {
             string strValue = (string)value;
             if (strValue.Length >= 0)
             {
                 enumValue = Convert.ToInt32(strValue);
             }
         }
         else
         {
             enumValue = Convert.ToInt32(value);
         }
     }
     return StandardEnumValueList(htmlHelper, name, enumType, filter, enumValue, optionLabel, includeZero, htmlAttributes);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// 根据 enumType 的指示绘制以 value 为默认值的下拉框。
 /// name 只用于控件标识及名称
 /// </summary>
 /// <param name="htmlHelper"></param>
 /// <param name="name"></param>
 /// <param name="enumType"></param>
 /// <param name="filter"></param>
 /// <param name="value"></param>
 /// <param name="optionLabel"></param>
 /// <param name="includeZero"></param>
 /// <returns></returns>
 public static MvcHtmlString EnumValueList(this HtmlHelper htmlHelper, string name,
     Type enumType, IEnumFilter filter, object value, string optionLabel, bool includeZero)
 {
     return EnumValueList(htmlHelper, name, enumType, filter, value, optionLabel, includeZero, null);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 根据 enumType 的指示绘制以 value 为默认值的下拉框。
 /// name 只用于控件标识及名称
 /// </summary>
 /// <param name="htmlHelper"></param>
 /// <param name="name"></param>
 /// <param name="enumType"></param>
 /// <param name="filter"></param>
 /// <param name="value"></param>
 /// <returns></returns>
 public static MvcHtmlString EnumValueList(this HtmlHelper htmlHelper, string name, Type enumType, IEnumFilter filter, object value)
 {
     return EnumValueList(htmlHelper, name, enumType, filter, value, null);
 }
Ejemplo n.º 8
0
        /// <summary>
        /// 根据 enumType 的指示绘制下拉框;在 ViewData 和 Model 中根据 name 查找属性,并将该属性值作为下拉框的默认值。
        /// 不要求属性必须存在
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <param name="enumType"></param>
        /// <param name="filter"></param>
        /// <param name="optionLabel"></param>
        /// <param name="includeZero"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString EnumValueList(this HtmlHelper htmlHelper, string name, Type enumType, IEnumFilter filter, string optionLabel, bool includeZero, object htmlAttributes)
        {
            object propertyValue = null;
            PropertyInfo propertyInfo = null;
            Type propertyType = ExtensionHelper.RetrieveProperty(htmlHelper, name, out propertyValue, out propertyInfo);

            return EnumValueList(htmlHelper, name, enumType, filter, propertyValue, optionLabel, includeZero, htmlAttributes);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// 根据 enumType 的指示绘制下拉框;在 ViewData 和 Model 中根据 name 查找属性,并将该属性值作为下拉框的默认值。
 /// 不要求属性必须存在
 /// </summary>
 /// <param name="htmlHelper"></param>
 /// <param name="name"></param>
 /// <param name="enumType"></param>
 /// <param name="filter"></param>
 /// <param name="optionLabel"></param>
 /// <returns></returns>
 public static MvcHtmlString EnumValueList(this HtmlHelper htmlHelper, string name, Type enumType, IEnumFilter filter, string optionLabel)
 {
     return EnumValueList(htmlHelper, name, enumType, filter, optionLabel, (object)null);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// 根据 enumType 的指示绘制下拉框;在 ViewData 和 Model 中根据 name 查找属性,并将该属性值作为下拉框的默认值。
 /// 不要求属性必须存在
 /// </summary>
 /// <param name="htmlHelper"></param>
 /// <param name="name"></param>
 /// <param name="enumType"></param>
 /// <param name="filter"></param>
 /// <returns></returns>
 public static string EnumValueList(this HtmlHelper htmlHelper, string name, Type enumType, IEnumFilter filter)
 {
     return EnumValueList(htmlHelper, name, enumType, filter, (string)null);
 }