Exemplo n.º 1
0
        internal static string ListItemToOption(GroupedSelectListItem item)
        {
            var tagBuilder = new TagBuilder("option")
            {
                InnerHtml = HttpUtility.HtmlEncode(item.Text)
            };

            if (item.Value != null)
            {
                tagBuilder.Attributes["value"] = item.Value;
            }
            if (item.Selected)
            {
                tagBuilder.Attributes["selected"] = "selected";
            }
            return(tagBuilder.ToString(TagRenderMode.Normal));
        }
Exemplo n.º 2
0
        private static MvcHtmlString SelectInternal(this HtmlHelper htmlHelper, string optionLabel, string name, IEnumerable <GroupedSelectListItem> selectList, bool allowMultiple, IDictionary <string, object> htmlAttributes)
        {
            name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Null Or Empty", nameof(name));
            }
            var flag1 = false;

            if (selectList == null)
            {
                selectList = htmlHelper.GetSelectData(name);
                flag1      = true;
            }
            var obj = allowMultiple ? htmlHelper.GetModelStateValue(name, typeof(string[])) : htmlHelper.GetModelStateValue(name, typeof(string));

            if (!flag1 && obj == null)
            {
                obj = htmlHelper.ViewData.Eval(name);
            }
            if (obj != null)
            {
                IEnumerable source;
                if (!allowMultiple)
                {
                    source = new object[1]
                    {
                        obj
                    }
                }
                ;
                else
                {
                    source = obj as IEnumerable;
                }
                var hashSet = new HashSet <string>(source.Cast <object>().Select(value => Convert.ToString(value, CultureInfo.CurrentCulture)), StringComparer.OrdinalIgnoreCase);
                var list    = new List <GroupedSelectListItem>();
                foreach (var groupedSelectListItem in selectList)
                {
                    groupedSelectListItem.Selected = groupedSelectListItem.Value != null?hashSet.Contains(groupedSelectListItem.Value) : hashSet.Contains(groupedSelectListItem.Text);

                    list.Add(groupedSelectListItem);
                }
                selectList = list;
            }
            var stringBuilder1 = new StringBuilder();

            if (optionLabel != null)
            {
                var stringBuilder2        = stringBuilder1;
                var groupedSelectListItem = new GroupedSelectListItem
                {
                    Text     = optionLabel,
                    Value    = string.Empty,
                    Selected = false
                };
                var str = ListItemToOption(groupedSelectListItem);
                stringBuilder2.AppendLine(str);
            }
            using (var enumerator = selectList.GroupBy(i => i.GroupKey).GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    var group = enumerator.Current;
                    var str   = selectList.Where(i => i.GroupKey == @group.Key).Select(it => it.GroupName).FirstOrDefault();
                    var flag2 = selectList.Where(i => i.GroupKey == @group.Key).Select(it => it.Disabled).FirstOrDefault();
                    stringBuilder1.AppendLine(string.Format(@"<optgroup label=""{0}"" value=""{1}"" {2}>", str, @group.Key, flag2 ? "disabled" : ""));
                    foreach (var groupedSelectListItem in @group)
                    {
                        stringBuilder1.AppendLine(ListItemToOption(groupedSelectListItem));
                    }
                    stringBuilder1.AppendLine("</optgroup>");
                }
            }
            var tagBuilder = new TagBuilder("select")
            {
                InnerHtml = stringBuilder1.ToString()
            };

            tagBuilder.MergeAttributes(htmlAttributes);
            tagBuilder.MergeAttribute("name", name, true);
            tagBuilder.GenerateId(name);
            if (allowMultiple)
            {
                tagBuilder.MergeAttribute("multiple", "multiple");
            }
            ModelState modelState;

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