当前位置: 首页>>代码示例>>C#>>正文


C# HtmlHelper.DropDownList方法代码示例

本文整理汇总了C#中System.Web.Mvc.HtmlHelper.DropDownList方法的典型用法代码示例。如果您正苦于以下问题:C# HtmlHelper.DropDownList方法的具体用法?C# HtmlHelper.DropDownList怎么用?C# HtmlHelper.DropDownList使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Web.Mvc.HtmlHelper的用法示例。


在下文中一共展示了HtmlHelper.DropDownList方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RenderDropDownForEnum

        public string RenderDropDownForEnum(HtmlHelper htmlHelper, PropertyInfo property, object value)
        {
            var items = GetDropDownItems(property);

            var dropDownHtml = htmlHelper
                .DropDownList(property.Name, items, UiTexts.Editor_DropDown_EmptyValue, new { @class = "form-control" })
                .ToHtmlString();

            dropDownHtml = this.SelectProperItem(dropDownHtml, value);

            if (property.GetAttribute<RequiredAttribute>() == null)
            {
                dropDownHtml = dropDownHtml.Replace("data-val=\"true\"", string.Empty);
            }

            var validationMessage = htmlHelper.ValidationMessage(property.Name, string.Empty, new { @class = "text-danger" });
            if (validationMessage != null)
            {
                dropDownHtml += validationMessage.ToHtmlString();
            }

            return dropDownHtml;
        }
开发者ID:dwdkls,项目名称:pizzamvc,代码行数:23,代码来源:DropDownRendererBase.cs

示例2: RenderSelectElement

        public static string RenderSelectElement(HtmlHelper html, BootstrapSelectElementModel model, BootstrapInputType inputType)
        {
            string combinedHtml = "{0}{1}{2}";
            if (model.selectedValue != null)
            {
                foreach (var item in model.selectList)
                {
                    if (item.Value == model.selectedValue.ToString())
                        item.Selected = true;
                }
            }

            model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));
            if (model.tooltipConfiguration != null) model.htmlAttributes.MergeHtmlAttributes(model.tooltipConfiguration.ToDictionary());
            if (model.tooltip != null) model.htmlAttributes.MergeHtmlAttributes(model.tooltip.ToDictionary());
            if (!string.IsNullOrEmpty(model.id)) model.htmlAttributes.AddOrReplace("id", model.id);

            // assign size class
            model.htmlAttributes.AddOrMergeCssClass("class", BootstrapHelper.GetClassForInputSize(model.size));

            // build html for input
            string input = string.Empty;

            if(inputType == BootstrapInputType.DropDownList)
                input = html.DropDownList(model.htmlFieldName, model.selectList, model.optionLabel, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString();

            if(inputType == BootstrapInputType.ListBox)
                input = html.ListBox(model.htmlFieldName, model.selectList, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString();

            // account for appendString, prependString, and AppendButtons
            TagBuilder appendPrependContainer = new TagBuilder("div");
            if (!string.IsNullOrEmpty(model.prependString) | !string.IsNullOrEmpty(model.appendString) | model.appendButtons.Count() > 0)
            {
                string addOnPrependString = "";
                string addOnAppendString = "";
                string addOnPrependButtons = "";
                string addOnAppendButtons = "";

                TagBuilder addOn = new TagBuilder("span");
                addOn.AddCssClass("add-on");
                if (!string.IsNullOrEmpty(model.prependString))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    addOn.InnerHtml = model.prependString;
                    addOnPrependString = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.appendString))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    addOn.InnerHtml = model.appendString;
                    addOnAppendString = addOn.ToString();
                }
                if (model.appendButtons.Count() > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    ((List<BootstrapButton>)model.appendButtons).ForEach(x => addOnAppendButtons += x.ToHtmlString());
                }
                if (model.prependButtons.Count() > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    ((List<BootstrapButton>)model.prependButtons).ForEach(x => addOnPrependButtons += x.ToHtmlString());
                }

                appendPrependContainer.InnerHtml = addOnPrependButtons + addOnPrependString + "{0}" + addOnAppendString + addOnAppendButtons;
                combinedHtml = appendPrependContainer.ToString(TagRenderMode.Normal) + "{1}{2}";
            }

            string helpText = model.helpText != null ? model.helpText.ToHtmlString() : string.Empty;
            string validationMessage = "";
            if(model.displayValidationMessage && html.ValidationMessage(model.htmlFieldName) != null )
            {
                string validation = html.ValidationMessage((string)model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
            }

            return MvcHtmlString.Create(string.Format(combinedHtml, input, helpText, validationMessage)).ToString();
        }
开发者ID:ngadotnet,项目名称:TwitterBootstrapMvc,代码行数:77,代码来源:Renderer.SelectElement.cs

示例3: Render

 /// <summary>
 /// Renders the specified HTML.
 /// </summary>
 /// <param name="html">The HTML helper.</param>
 /// <param name="name">The element name.</param>
 /// <param name="value">The element value.</param>
 /// <param name="values">The element values.</param>
 /// <returns>Returns element html code.</returns>
 public override string Render(HtmlHelper html, string name, string value, string values)
 {
     return html.DropDownList(name, ParseValues(Gender.Female + "," + Gender.Male, value)).ToString();
 }
开发者ID:coreframework,项目名称:Core-Framework,代码行数:12,代码来源:GenderAttribute.cs

示例4: RenderSelectElement

        public static HtmlString RenderSelectElement(HtmlHelper html, SelectElementModel model, InputType inputType)
        {
            var combinedHtml = "{0}{1}{2}";
            var input = string.Empty;

            if (model.selectedValue != null)
            {
                foreach (var item in model.selectList)
                {
                    if (item.Value == model.selectedValue.ToString())
                        item.Selected = true;
                }
            }

            model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));

            if (!string.IsNullOrEmpty(model.id)) model.htmlAttributes.AddOrReplace("id", model.id);

            if (model.size != Size._NotSet)
                model.htmlAttributes.AddOrMergeCssClass("class", $"input-{model.size.ToName()}");

            model.htmlAttributes.AddOrMergeCssClass("class", "form-control");

            // build html for input

            if (inputType == InputType.DropDownList)
                input =
                    html.DropDownList(model.htmlFieldName, model.selectList, model.optionLabel,
                        model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString();

            if (inputType == InputType.ListBox)
                input =
                    html.ListBox(model.htmlFieldName, model.selectList, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString();

            // account for appendString, prependString, and AppendButtons

            if (!string.IsNullOrEmpty(model.prependString) || !string.IsNullOrEmpty(model.appendString))
            {
                var appendPrependContainer = new TagBuilder("div");
                var addOn = new TagBuilder("span");

                var addOnPrependString = "";
                var addOnAppendString = "";
                var addOnPrependButtons = "";
                var addOnAppendButtons = "";

                appendPrependContainer.AddOrMergeCssClass("input-group");
                addOn.AddCssClass("input-group-addon");

                if (!string.IsNullOrEmpty(model.prependString))
                {
                    addOn.InnerHtml = model.prependString;
                    addOnPrependString = addOn.ToString();
                }

                if (!string.IsNullOrEmpty(model.appendString))
                {
                    addOn.InnerHtml = model.appendString;
                    addOnAppendString = addOn.ToString();
                }

                appendPrependContainer.InnerHtml = addOnPrependButtons + addOnPrependString + "{0}" + addOnAppendString +
                                                   addOnAppendButtons;
                combinedHtml = appendPrependContainer.ToString(TagRenderMode.Normal) + "{1}{2}";
            }

            var helpText = model.helpText != null ? model.helpText.ToHtmlString() : string.Empty;
            var validationMessage = "";

            if (model.displayValidationMessage)
            {
                var validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString();
                validationMessage = new HelpText(validation, model.validationMessageStyle).ToHtmlString();
            }

            var inputElement = string.Format(combinedHtml, input, helpText, validationMessage);

            TagBuilder inputWrapper = null;

            if (!string.IsNullOrEmpty(model.inputElementWrapper))
            {
                inputWrapper = new TagBuilder(model.inputElementWrapper);

                if (model.inputElementWrapperAttributes != null)
                    inputWrapper.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(model.inputElementWrapperAttributes));

                inputWrapper.InnerHtml = inputElement;
            }

            var htmlString = inputWrapper != null
                ? inputWrapper.ToString(TagRenderMode.Normal)
                : inputElement;

            return new HtmlString(htmlString);
        }
开发者ID:andrewreyes,项目名称:NiftyMvcHelpers,代码行数:95,代码来源:RenderSelectElement.cs


注:本文中的System.Web.Mvc.HtmlHelper.DropDownList方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。