private static string CreateOption(SelectListItemTree item, int level, string indentation) { StringBuilder htmlBuilder = new StringBuilder(); TagBuilder option = new TagBuilder("option"); option.InnerHtml = GetIndentation(level, indentation) + System.Web.HttpUtility.HtmlEncode(item.Text); if (item.Value != null) { option.Attributes["Value"] = item.Value; } if (item.Selected) { option.Attributes["selected"] = "selected"; } htmlBuilder.AppendLine(option.ToString()); if (item.Items != null) { ++level; foreach (var i in item.Items) { htmlBuilder.AppendLine(CreateOption(i, level, indentation)); } } return(htmlBuilder.ToString()); }
public static IHtmlString DropDownListTree(this HtmlHelper htmlHelper, string name, IEnumerable <SelectListItemTree> selectListItemTree, IDictionary <string, object> htmlAttributes, bool allowMutiple, string optionLabel, string indentation) { ModelState state; name = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); if (string.IsNullOrEmpty(name)) { throw new ArgumentException("NULL", "name"); } int level = 0; TagBuilder selectBuilder = new TagBuilder("select"); StringBuilder optionBuilder = new StringBuilder(); //var blankOption = new TagBuilder("option"); //optionBuilder.AppendLine(blankOption.ToString()); if (optionLabel != null) { SelectListItemTree item = new SelectListItemTree(); item.Text = optionLabel; item.Value = string.Empty; optionBuilder.AppendLine(CreateOption(item, level, indentation)); } foreach (var item in selectListItemTree) { optionBuilder.AppendLine(CreateOption(item, level, indentation)); } selectBuilder.InnerHtml = optionBuilder.ToString(); selectBuilder.MergeAttributes <string, object>(htmlAttributes); selectBuilder.MergeAttribute("name", name, true); selectBuilder.GenerateId(name); if (allowMutiple) { selectBuilder.MergeAttribute("mutiple", "mutiple"); } if (htmlHelper.ViewData.ModelState.TryGetValue(name, out state) && (state.Errors.Count > 0)) { selectBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName); } return(new HtmlString(selectBuilder.ToString())); }