Exemple #1
0
        internal static string ListItemToOption(SelectListItem item)
        {
            TagBuilder tagBuilder = new TagBuilder("option")
            {
                InnerHtml = HttpUtility.HtmlEncode(item.Text)
            };
            TagBuilder value = tagBuilder;

            if (item.Value != null)
            {
                value.Attributes["value"] = item.Value;
            }
            if (item.Selected)
            {
                value.Attributes["selected"] = "selected";
            }
            if (item.Disabled)
            {
                value.Attributes["disabled"] = "disabled";
            }
            if (!string.IsNullOrEmpty(item.DataCode))
            {
                value.Attributes["data-code"] = item.DataCode;
            }
            return(value.ToString(TagRenderMode.Normal));
        }
Exemple #2
0
        private static MvcHtmlString SelectInternal(this HtmlHelper htmlHelper, ModelMetadata metadata, string optionLabel, string name, IEnumerable <SelectListItem> selectList, bool allowMultiple, IDictionary <string, object> htmlAttributes)
        {
            ModelState modelState;
            string     fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name);

            if (string.IsNullOrEmpty(fullHtmlFieldName))
            {
                throw new ArgumentException("Cannot be null", "name");
            }
            bool flag = false;

            if (selectList == null)
            {
                selectList = htmlHelper.GetSelectData(name);
                flag       = true;
            }
            object model = (allowMultiple ? htmlHelper.GetModelStateValue(fullHtmlFieldName, typeof(string[])) : htmlHelper.GetModelStateValue(fullHtmlFieldName, typeof(string)));

            if (model == null && !string.IsNullOrEmpty(name))
            {
                if (!flag)
                {
                    model = htmlHelper.ViewData.Eval(name);
                }
                else if (metadata != null)
                {
                    model = metadata.Model;
                }
            }
            if (model != null)
            {
                selectList = GetSelectListWithDefaultValue(selectList, model, allowMultiple);
            }
            StringBuilder stringBuilder = new StringBuilder();

            if (optionLabel != null)
            {
                SelectListItem selectListItem = new SelectListItem()
                {
                    Text     = optionLabel,
                    Value    = string.Empty,
                    Selected = false
                };
                stringBuilder.AppendLine(ListItemToOption(selectListItem));
            }
            foreach (SelectListItem selectListItem1 in selectList)
            {
                stringBuilder.AppendLine(ListItemToOption(selectListItem1));
            }
            TagBuilder tagBuilder = new TagBuilder("select")
            {
                InnerHtml = stringBuilder.ToString()
            };
            TagBuilder tagBuilder1 = tagBuilder;

            tagBuilder1.MergeAttributes <string, object>(htmlAttributes);
            tagBuilder1.MergeAttribute("name", fullHtmlFieldName, true);
            tagBuilder1.GenerateId(fullHtmlFieldName);
            if (allowMultiple)
            {
                tagBuilder1.MergeAttribute("multiple", "multiple");
            }
            if (htmlHelper.ViewData.ModelState.TryGetValue(fullHtmlFieldName, out modelState) && modelState.Errors.Count > 0)
            {
                tagBuilder1.AddCssClass(HtmlHelper.ValidationInputCssClassName);
            }
            tagBuilder1.MergeAttributes <string, object>(htmlHelper.GetUnobtrusiveValidationAttributes(name, metadata));
            return(new MvcHtmlString(tagBuilder1.ToString(TagRenderMode.Normal)));
        }