예제 #1
0
        public static MvcHtmlString ValueBoxForEnum <TEnum>(this HtmlHelper htmlHelper, string name, IEnumerable <TEnum> values, string optionLabel, object htmlAttributes = null)
            where TEnum : struct
        {
            var list = values.Select(e => new SelectListItem {
                Value = (e as Enum).ToString("d"), Text = e.ToString()
            });

            return(SelectExtensions.DropDownList(htmlHelper, name, list, optionLabel, htmlAttributes));
        }
예제 #2
0
        /// <summary>
        /// 适用于Value和Text是一个数字值的下拉选单(Text值和value值一致)
        /// </summary>
        /// <typeparam name="TEnum"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="name"></param>
        /// <param name="optionLabel"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString ValueBoxForEnumYearsAdd <TEnum>(this HtmlHelper htmlHelper,
                                                                    string name, string optionLabel, object htmlAttributes = null)
            where TEnum : struct
        {
            var values = Enum.GetValues(typeof(TEnum)).Cast <TEnum>();
            var list   = values.Select(e => new SelectListItem {
                Value = e.ToString(), Text = (e as Enum).ToString("d")
            });

            return(SelectExtensions.DropDownList(htmlHelper, name, list, optionLabel, htmlAttributes));
        }
예제 #3
0
        public static MvcHtmlString ValueBoxForEnum <T, TEnum>(this HtmlHelper <T> htmlHelper,
                                                               Expression <Func <T, TEnum> > expression, TEnum?defaultValue, string optionlable, object htmlAttributes = null)
            where TEnum : struct
        {
            var values = Enum.GetValues(typeof(TEnum)).Cast <TEnum>();
            var list   = values.Select(e => new SelectListItem {
                Value    = e.ToString(), Text = (e as Enum).ToString("d"),
                Selected = defaultValue.HasValue && e.Equals(defaultValue.Value)
            });

            return(SelectExtensions.DropDownListFor(htmlHelper, expression, list, optionlable, htmlAttributes));
        }
예제 #4
0
        /// <summary>
        /// 在EditorFor中的DropDownListFor採用此方法,否則不會加載model數據
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <param name="htmlHelper"></param>
        /// <param name="expression"></param>
        /// <param name="selectList"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static MvcHtmlString EditorDropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, object htmlAttributes)
        {
            var dropDown = SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, htmlAttributes);
            var model    = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData).Model;

            if (model == null)
            {
                return(dropDown);
            }
            var dropDownWithSelect = dropDown.ToString().Replace("value=\"" + model.ToString() + "\"", "value=\"" + model.ToString() + "\" selected");

            return(MvcHtmlString.Create(dropDownWithSelect));
        }
        private static string BooleanTemplateDropDownList(bool?value)
        {
            StringBuilder builder = new StringBuilder();

            TagBuilder selectTag = new TagBuilder("select");

            selectTag.AddCssClass("list-box");
            selectTag.AddCssClass("tri-state");
            selectTag.Attributes["disabled"] = "disabled";
            builder.Append(selectTag.ToString(TagRenderMode.StartTag));

            foreach (SelectListItem item in DefaultEditorTemplates.TriStateValues(value))
            {
                builder.Append(SelectExtensions.ListItemToOption(item));
            }

            builder.Append(selectTag.ToString(TagRenderMode.EndTag));
            return(builder.ToString());
        }
        public static MvcHtmlString MyDropDownListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, string optionLabel, IDictionary <string, object> htmlAttributes)
        {
            if (expression == null)
            {
                throw new ArgumentNullException("expression");
            }

            ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

            IDictionary <string, object> validationAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(ExpressionHelper.GetExpressionText(expression), metadata);

            if (htmlAttributes == null)
            {
                htmlAttributes = validationAttributes;
            }
            else
            {
                htmlAttributes = htmlAttributes.Concat(validationAttributes).ToDictionary(k => k.Key, v => v.Value);
            }

            return(SelectExtensions.DropDownListFor(htmlHelper, expression, selectList, optionLabel, htmlAttributes));
        }