Пример #1
0
        public static MvcHtmlString CheckBoxListForEx <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, SelectListLayout layout, IDictionary <string, object> htmlAttributes)
        {
            IEnumerable <IInputViewModifier> modifier;
            var metadata = ModelMetadata.FromLambdaExpression <TModel, TProperty>(expression, htmlHelper.ViewData);

            if (metadata != null && metadata.TryGetExtent <IEnumerable <IInputViewModifier> >(out modifier))
            {
                modifier.MapInputToHtmlAttributes(ref expression, ref htmlAttributes);
            }
            return(htmlHelper.CheckBoxListFor <TModel, TProperty>(expression, selectList, layout, htmlAttributes));
        }
Пример #2
0
 public static MvcHtmlString RadioButtonListFor <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, SelectListLayout layout, IDictionary <string, object> htmlAttributes)
 {
     if (expression == null)
     {
         throw new ArgumentNullException("expression");
     }
     return(RadioButtonListHelper(htmlHelper, ExpressionHelper.GetExpressionText(expression), selectList, layout, htmlAttributes));
 }
Пример #3
0
 public static MvcHtmlString CheckBoxListForEx <TModel, TProperty>(this HtmlHelper <TModel> htmlHelper, Expression <Func <TModel, TProperty> > expression, IEnumerable <SelectListItem> selectList, SelectListLayout layout)
 {
     return(CheckBoxListForEx <TModel, TProperty>(htmlHelper, expression, selectList, layout, ((IDictionary <string, object>)null)));
 }
Пример #4
0
 public static MvcHtmlString RadioButtonList(this HtmlHelper htmlHelper, string name, SelectListLayout layout)
 {
     return(RadioButtonList(htmlHelper, name, null, layout, ((IDictionary <string, object>)null)));
 }
Пример #5
0
 public static MvcHtmlString RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList, SelectListLayout layout, object htmlAttributes)
 {
     return(RadioButtonList(htmlHelper, name, selectList, layout, ((IDictionary <string, object>) new RouteValueDictionary(htmlAttributes))));
 }
Пример #6
0
        internal static string HtmlForListItemToOption(string name, SelectListItem item, SelectListLayout layout, bool allowMultiple)
        {
            var inputTag = new TagBuilder("input");

            inputTag.Attributes.Add("type", (allowMultiple ? "checkbox" : "radio"));
            inputTag.Attributes.Add("name", name);
            if (item.Value != null)
            {
                inputTag.Attributes["value"] = item.Value;
            }
            if (item.Selected)
            {
                inputTag.Attributes["checked"] = "checked";
            }
            //
            var labelTag = new TagBuilder("label");

            labelTag.Attributes.Add("for", name);
            if (item.Text != null)
            {
                labelTag.SetInnerText(item.Text);
            }
            //
            var divTag = new TagBuilder("div");

            if (layout.RepeatDirection == RepeatDirection.Horizontal)
            {
                divTag.AddCssStyle("float", "left");
            }
            divTag.AddCssClass((allowMultiple ? "checkbox" : "radio") + "List");
            divTag.InnerHtml = inputTag.ToString(TagRenderMode.Normal) + labelTag.ToString(TagRenderMode.Normal);
            return(divTag.ToString(TagRenderMode.Normal));
        }
Пример #7
0
        private static string HtmlForSelectList(string name, IEnumerable <SelectListItem> items, SelectListLayout layout, bool allowMultiple)
        {
            var b = new StringBuilder();

            foreach (var item in items)
            {
                b.AppendLine(HtmlForListItemToOption(name, item, layout, allowMultiple));
            }
            return(b.ToString());
        }
Пример #8
0
        private static MvcHtmlString SelectInternal(HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> items, SelectListLayout layout, bool allowMultiple, IDictionary <string, object> htmlAttributes)
        {
            ModelState state;

            name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("MvcResources.Common_NullOrEmpty", "name");
            }
            var hasItems = false;

            if (items == null)
            {
                items    = GetSelectData(htmlHelper, name);
                hasItems = true;
            }
            object data = (allowMultiple ? htmlHelper.GetModelStateValue(name, typeof(string[])) : htmlHelper.GetModelStateValue(name, typeof(string)));

            if (!hasItems && data == null)
            {
                data = htmlHelper.ViewData.Eval(name);
            }
            if (data != null)
            {
                var source = (allowMultiple ? (data as IEnumerable) : ((IEnumerable) new object[] { data }));
                var set    = new HashSet <string>(source.Cast <object>().Select <object, string>(value => Convert.ToString(value, CultureInfo.CurrentCulture)), StringComparer.OrdinalIgnoreCase);
                foreach (var item in items)
                {
                    item.Selected = (item.Value != null ? set.Contains(item.Value) : set.Contains(item.Text));
                }
            }
            var b = new TagBuilder("div");

            if (layout.RepeatDirection == RepeatDirection.Horizontal)
            {
                b.AddCssStyle("clear", "both");
            }
            b.InnerHtml = HtmlForSelectList(name, items, layout, allowMultiple);
            b.MergeAttributes <string, object>(htmlAttributes);
            b.GenerateId(name);
            if (htmlHelper.ViewData.ModelState.TryGetValue(name, out state) && (state.Errors.Count > 0))
            {
                b.AddCssClass(HtmlHelper.ValidationInputCssClassName);
            }
            return(b.ToMvcHtmlString(TagRenderMode.Normal));
        }
Пример #9
0
 private static MvcHtmlString CheckBoxListHelper(HtmlHelper htmlHelper, string expression, IEnumerable <SelectListItem> selectList, SelectListLayout layout, IDictionary <string, object> htmlAttributes)
 {
     return(SelectInternal(htmlHelper, expression, selectList, (layout ?? new SelectListLayout()), true, htmlAttributes));
 }
Пример #10
0
 public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItem> selectList, SelectListLayout layout, IDictionary <string, object> htmlAttributes)
 {
     return(CheckBoxList(htmlHelper, name, selectList, layout, htmlAttributes));
 }