Esempio n. 1
0
        public static MvcHtmlString DropDownForEnum <EnumType>(this HtmlHelper html, string name, EnumType?value, object htmlAttributes = null) where EnumType : struct
        {
            var type = typeof(EnumType);

            if (!type.IsEnum)
            {
                throw new Exception("The member must be an Enum");
            }
            var builder = new TagBuilder("select");

            builder.GenerateId(name);
            builder.MergeAttribute("name", name);
            foreach (var n in Enum.GetNames(type))
            {
                var        title   = TitleAttribute.GetTitle(type.GetField(n, BindingFlags.Public | BindingFlags.Static), n);
                TagBuilder op      = new TagBuilder("option");
                var        thisVal = (int)Enum.Parse(type, n);
                op.Attributes["value"] = thisVal.ToString();
                //if (value != null && (int)value.Value == thisVal)
                //  op.Attributes["selected"] = "selected";
                op.SetInnerText(title);
                builder.InnerHtml += op.ToString();
            }
            if (htmlAttributes != null)
            {
                foreach (var prop in htmlAttributes.GetType().GetProperties(
                             BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    builder.Attributes[prop.Name] = prop.GetValue(htmlAttributes, null).ToString();
                }
            }
            return(MvcHtmlString.Create(builder.ToString()));
        }
Esempio n. 2
0
        static void CreateEnumColumn <TContext, TEntity>(GridColumnFactory <TEntity> cols, KeyValuePair <PropertyInfo, Attribute[]> p) where TContext : CachableDbContext <TContext>, new()
            where TEntity : class
        {
            List <KeyValuePair <int, string> > data = new List <KeyValuePair <int, string> >();
            var values = Enum.GetValues(p.Key.PropertyType);

            foreach (var v in values)
            {
                var name  = Enum.GetName(p.Key.PropertyType, v);
                var title = TitleAttribute.GetTitle(p.Key.PropertyType, name);
                if (string.IsNullOrEmpty(title))
                {
                    title = name;
                }
                data.Add(new KeyValuePair <int, string>((int)v, title));
            }
            var col = cols.ForeignKey(p.Key.Name, data, "Key", "Value");

            SetColumnProperties(p.Key, p.Value, col);
        }
Esempio n. 3
0
        public static MvcHtmlString DropDownForEnum <TModel, TProperty>(this HtmlHelper <TModel> html
                                                                        , Expression <Func <TModel, TProperty> > expression, object htmlAttributes = null)
        {
            var memberInfo = expression.MemberInfo();
            var type       = ((PropertyInfo)memberInfo).PropertyType;

            if (!type.IsEnum)
            {
                throw new Exception("The member must be an Enum");
            }
            var builder = new TagBuilder("select");

            builder.GenerateId(memberInfo.Name);
            builder.MergeAttribute("name", expression.MemberName());
            object value = GetValue(html, expression);

            foreach (var name in Enum.GetNames(type))
            {
                var        title   = TitleAttribute.GetTitle(type.GetField(name, BindingFlags.Public | BindingFlags.Static), name);
                TagBuilder op      = new TagBuilder("option");
                var        thisVal = (int)Enum.Parse(type, name);
                op.Attributes["value"] = thisVal.ToString();
                if (value != null && (int)value == thisVal)
                {
                    op.Attributes["selected"] = "selected";
                }
                op.SetInnerText(title);
                builder.InnerHtml += op.ToString();
            }
            if (htmlAttributes != null)
            {
                foreach (var prop in htmlAttributes.GetType().GetProperties(
                             BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
                {
                    builder.Attributes[prop.Name] = prop.GetValue(htmlAttributes, null).ToString();
                }
            }
            return(MvcHtmlString.Create(builder.ToString()));
        }
Esempio n. 4
0
 static string GetTitle(ICustomAttributeProvider typeMember, string name = null, Type containingType = null)
 {
     return(TitleAttribute.GetTitle(typeMember, name, containingType));
 }