Пример #1
0
        internal static MvcHtmlString MinMaxBase <VM>(
            this HtmlHelper <VM> htmlHelper,
            string type,
            string name,
            object value,
            ModelMetadata metaData = null,
            IDictionary <string, object> htmlAttributes = null)
        {
            string partialName = name;

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            TagBuilder tagBuilder = new TagBuilder("input");

            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("name", name, true /* replaceExisting */);
            tagBuilder.MergeAttribute("type", type, true /* replaceExisting */);
            tagBuilder.MergeAttribute("value", Convert.ToString(value), true /* replaceExisting */);
            tagBuilder.GenerateId(name);
            ModelState modelState;

            if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
                }
            }
            IDictionary <String, object> validation = MvcEnvironment.GetUnobtrusiveValidation(htmlHelper, name, metaData);

            if (validation != null)
            {
                if (validation.ContainsKey("data-val-dynamicrange-min"))
                {
                    tagBuilder.MergeAttribute("min", Convert.ToString(validation["data-val-dynamicrange-min"]), false);
                }
                if (validation.ContainsKey("data-val-dynamicrange-max"))
                {
                    tagBuilder.MergeAttribute("max", Convert.ToString(validation["data-val-dynamicrange-max"]), false);
                }
            }
            return(MvcHtmlString.Create(tagBuilder.ToString()));
        }
Пример #2
0
        internal static MvcHtmlString DropDownbase <VM, TItem, TDisplay, TValue>(
            this HtmlHelper <VM> htmlHelper, string name, object value, List <ExtendedSelectListItem> items, IDictionary <string, object> htmlAttributes, bool allowsMultipleValues, bool useGroups, ModelMetadata metaData = null)
        {
            string partialName = name;

            if (name == null)
            {
                throw new ArgumentNullException("name");
            }
            name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (items == null)
            {
                throw new ArgumentNullException("items");
            }
            object defaultValue = (allowsMultipleValues) ? htmlHelper.GetModelStateValue(name, typeof(string[])) : htmlHelper.GetModelStateValue(name, typeof(string));

            if (defaultValue == null)
            {
                defaultValue = value;
            }
            if (defaultValue != null)
            {
                IEnumerable          defaultValues  = (allowsMultipleValues) ? defaultValue as IEnumerable : new[] { defaultValue };
                IEnumerable <string> values         = (from object currValue in defaultValues select Convert.ToString(currValue, CultureInfo.CurrentCulture)).ToList();
                HashSet <string>     selectedValues = new HashSet <string>(values, StringComparer.OrdinalIgnoreCase);

                foreach (ExtendedSelectListItem item in items)
                {
                    item.Selected = (item.Value != null) ? selectedValues.Contains(item.Value) : selectedValues.Contains(item.Text);
                }
            }
            StringBuilder listItemBuilder = new StringBuilder();

            if (useGroups)
            {
                foreach (var group in items.GroupBy(i => i.GroupKey))
                {
                    var groupProps = group.Select(it => new { Name = it.GroupName, Attrs = it.GroupAttributes }).FirstOrDefault();
                    IDictionary <string, object> dictionary = groupProps.Attrs as IDictionary <string, object>;
                    if (group.Key != null && groupProps.Name != null)
                    {
                        if (dictionary != null)
                        {
                            listItemBuilder.AppendLine(string.Format("<optgroup label=\"{0}\" value=\"{1}\" {2}>",
                                                                     htmlHelper.Encode(groupProps.Name),
                                                                     htmlHelper.Encode(group.Key),
                                                                     BasicHtmlHelper.GetAttributesString(dictionary)));
                        }
                        else
                        {
                            listItemBuilder.AppendLine(string.Format("<optgroup label=\"{0}\" value=\"{1}\" {2}>",
                                                                     htmlHelper.Encode(groupProps.Name),
                                                                     htmlHelper.Encode(group.Key),
                                                                     BasicHtmlHelper.GetAttributesString(groupProps.Attrs)));
                        }
                    }
                    foreach (ExtendedSelectListItem item in group)
                    {
                        listItemBuilder.AppendLine(ListItemToOption(item));
                    }
                    if (group.Key != null && groupProps.Name != null)
                    {
                        listItemBuilder.AppendLine("</optgroup>");
                    }
                }
            }
            else
            {
                foreach (ExtendedSelectListItem item in items)
                {
                    listItemBuilder.AppendLine(ListItemToOption(item));
                }
            }

            TagBuilder tagBuilder = new TagBuilder("select")
            {
                InnerHtml = listItemBuilder.ToString()
            };

            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("name", name, true /* replaceExisting */);
            tagBuilder.GenerateId(name);
            if (allowsMultipleValues)
            {
                tagBuilder.MergeAttribute("multiple", "multiple");
            }

            ModelState modelState;

            if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState))
            {
                if (modelState.Errors.Count > 0)
                {
                    tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);
                }
            }
            tagBuilder.MergeAttributes(MvcEnvironment.GetUnobtrusiveValidation(htmlHelper, partialName, metaData));
            return(MvcHtmlString.Create(tagBuilder.ToString()));
        }