本文整理汇总了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;
}
示例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();
}
示例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();
}
示例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);
}