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


C# HtmlHelper.GetUnobtrusiveValidationAttributes方法代码示例

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


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

示例1: RenderCheckBoxCustom

        public static HtmlString RenderCheckBoxCustom(HtmlHelper html, CheckBoxModel model)
        {
            var fullHtmlFieldName = html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(model.htmlFieldName);

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

            ModelState modelState;

            if (html.ViewData.ModelState.TryGetValue(fullHtmlFieldName, out modelState))
            {
                if (modelState.Errors.Count > 0)
                    model.htmlAttributes.AddOrMergeCssClass("class", "input-validation-error");

                if (modelState.Value != null && ((string[]) modelState.Value.RawValue).Contains(model.value.ToString()))
                    model.isChecked = true;
            }

            var input = new TagBuilder("input");
            input.Attributes.Add("type", "checkbox");
            input.Attributes.Add("name", fullHtmlFieldName);
            input.Attributes.Add("id", model.id);
            input.Attributes.Add("value", model.value.ToString());

            if (model.isChecked)
                input.Attributes.Add("checked", "checked");

            if (model.isDisabled)
                input.Attributes.Add("disabled", "disabled");

            input.MergeAttributes(model.htmlAttributes.FormatHtmlAttributes());

            return new HtmlString(input.ToString(TagRenderMode.SelfClosing));
        }
开发者ID:andrewreyes,项目名称:NiftyMvcHelpers,代码行数:33,代码来源:RenderCheckBoxCustom.cs

示例2: RenderCheckBox

        public static string RenderCheckBox(HtmlHelper html, BootstrapCheckBoxModel model)
        {
            model.htmlAttributes.AddRange(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));
            if (model.tooltipConfiguration != null) model.htmlAttributes.AddRange(model.tooltipConfiguration.ToDictionary());
            var mergedHtmlAttrs = string.IsNullOrEmpty(model.id) ? model.htmlAttributes : model.htmlAttributes.AddOrReplace("id", model.id);

            string validationMessage = "";
            if (model.displayValidationMessage)
            {
                string validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
            }
            return html.CheckBox(model.htmlFieldName, model.isChecked, mergedHtmlAttrs.FormatHtmlAttributes()).ToHtmlString() + validationMessage;
        }
开发者ID:bbqchickenrobot,项目名称:TwitterBootstrapMvc,代码行数:14,代码来源:Renderer.CheckBox.cs

示例3: RenderRadioButton

        public static string RenderRadioButton(HtmlHelper html, BootstrapRadioButtonModel model)
        {
            if (model.tooltipConfiguration != null) model.htmlAttributes.MergeHtmlAttributes(model.tooltipConfiguration.ToDictionary());

            model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));
            if (!string.IsNullOrEmpty(model.id)) model.htmlAttributes.AddOrReplace("id", model.id);

            string validationMessage = "";
            if (model.displayValidationMessage)
            {
                string validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
            }
            return html.RadioButton(model.htmlFieldName, model.value, model.isChecked, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString() + validationMessage;
        }
开发者ID:Boggin,项目名称:TwitterBootstrapMvc,代码行数:15,代码来源:Renderer.RadioButton.cs

示例4: RenderTextArea

        public static string RenderTextArea(HtmlHelper html, BootstrapTextAreaModel model)
        {
            if (model == null || string.IsNullOrEmpty(model.htmlFieldName)) return null;

            string validationMessage = "";
            if (model.displayValidationMessage)
            {
                string validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
            }
            model.htmlAttributes.AddRange(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));
            if(!string.IsNullOrEmpty(model.id)) model.htmlAttributes.AddOrReplace("id", model.id);
            if (model.tooltipConfiguration != null) model.htmlAttributes.AddRange(model.tooltipConfiguration.ToDictionary());
            return html.TextArea(model.htmlFieldName, model.value, model.rows, model.columns, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString() + validationMessage;
        }
开发者ID:bbqchickenrobot,项目名称:TwitterBootstrapMvc,代码行数:15,代码来源:Renderer.TextArea.cs

示例5: RenderCheckBox

        public static HtmlString RenderCheckBox(HtmlHelper html, CheckBoxModel model)
        {
            model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));

            var mergedHtmlAttrs = string.IsNullOrEmpty(model.id) ? model.htmlAttributes : model.htmlAttributes.AddOrReplace("id", model.id);

            string validationMessage = "";

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

            return new HtmlString(html.CheckBox(model.htmlFieldName, model.isChecked, mergedHtmlAttrs.FormatHtmlAttributes()).ToHtmlString() + validationMessage);
        }
开发者ID:andrewreyes,项目名称:NiftyMvcHelpers,代码行数:16,代码来源:RenderCheckBox.cs

示例6: RenderFile

        public static string RenderFile(HtmlHelper html, BootstrapFileModel model)
        {
            if (model == null || string.IsNullOrEmpty(model.htmlFieldName)) return null;

            string validationMessage = "";
            if (model.displayValidationMessage)
            {
                string validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
            }

            model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));
            if (model.tooltipConfiguration != null) model.htmlAttributes.MergeHtmlAttributes(model.tooltipConfiguration.ToDictionary());
            var mergedHtmlAttrs = model.htmlAttributes.FormatHtmlAttributes().AddOrReplace("type", "File");
            if (!string.IsNullOrEmpty(model.id)) mergedHtmlAttrs.AddOrReplace("id", model.id);

            return html.TextBox(model.htmlFieldName, null, mergedHtmlAttrs).ToHtmlString() + validationMessage;
        }
开发者ID:Boggin,项目名称:TwitterBootstrapMvc,代码行数:18,代码来源:Renderer.File.cs

示例7: RenderTextArea

        public static HtmlString RenderTextArea(HtmlHelper html, TextAreaModel model)
        {
            var validationMessage = "";

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

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

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

            if (!string.IsNullOrEmpty(model.placeholder))
                model.htmlAttributes.Add("placeholder", model.placeholder);

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

            var htmlTextArea =
                html.TextArea(model.htmlFieldName, model.value, model.rows, model.columns,
                    model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString() + 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 = htmlTextArea;
            }

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

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

示例8: RenderRadioButton

        public static HtmlString RenderRadioButton(HtmlHelper html, RadioButtonModel model)
        {
            model.htmlAttributes.MergeHtmlAttributes(html.GetUnobtrusiveValidationAttributes(model.htmlFieldName, model.metadata));

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

            var validationMessage = "";

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

            return
                new HtmlString(
                    html.RadioButton(model.htmlFieldName, model.value, model.isChecked, model.htmlAttributes.FormatHtmlAttributes())
                        .ToHtmlString() + validationMessage);
        }
开发者ID:andrewreyes,项目名称:NiftyMvcHelpers,代码行数:20,代码来源:RenderRadioButton.cs

示例9: ObjectTemplate

        internal static IHtmlString ObjectTemplate(HtmlHelper html, 
            object formattedValue, 
            string htmlFieldName,
            ModelMetadata modelMetadata, 
            IDictionary<string, object> htmlAttributes)
        {
            var viewData = html.ViewContext.ViewData;
            var templateInfo = viewData.TemplateInfo;
            var containerData = html.ViewDataContainer.ViewData;

            var formContext = html.ViewContext.FormContext;
            var isHorisontalForm = (formContext is CarcassMvcFormContext) &&
                ((formContext as CarcassMvcFormContext).FormClass ?? String.Empty).HasCssClass (CarcassMvcSettings.BootsrapFormClassHorisontal);

            var sb = new StringBuilder();
            if (templateInfo.TemplateDepth > 1)
            {
                if (modelMetadata.Model != null)
                    return MvcHtmlString.Create(modelMetadata.SimpleDisplayText);
                else
                    return MvcHtmlString.Create(modelMetadata.NullDisplayText);
            }

            var inlineValidation = htmlAttributes.Get("InlineValidation", true);
            var validationClass = htmlAttributes.Get("ValidationClass", CarcassMvcSettings.ValidationMessageClass);
            var validationAttrs = new Dictionary<string, object>() { { "class", validationClass } };

            var editorAttributes = new Dictionary<string, object>();
            if (htmlAttributes.ContainsKey("EditorClass"))
                editorAttributes.Add("class", htmlAttributes["EditorClass"]);

            var content = new TagBuilder("div");
            if (htmlAttributes.ContainsKey("CssClass"))
                content.AddCssClass(htmlAttributes["CssClass"] as string);

            sb.Append(content.ToString(TagRenderMode.StartTag));

            // setup controls hierarchy
            var initHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
            if (!String.IsNullOrEmpty(htmlFieldName))
                templateInfo.HtmlFieldPrefix = templateInfo.GetFullHtmlFieldName(htmlFieldName);

            try
            {
                foreach (var metadata in modelMetadata.Properties.Where(pm => EditorTemplates.ShouldShow(pm, templateInfo)))
                {
                    var fieldType = metadata.TemplateHint ?? metadata.DataTypeName;
                    var isHidden = fieldType == "HiddenInput";
                    var itemFieldName = metadata.PropertyName;

                    if (!isHidden && !metadata.HideSurroundingHtml)
                    {
                        var labelAttributes = isHorisontalForm
                            ? new Dictionary<string, object> { { "class", CarcassMvcSettings.BootsrapFormLabelClass } } : null;
                        var label = LabelExtensions.FormatCarcassLabel(html, metadata, itemFieldName, null, labelAttributes);

                        if (isHorisontalForm)
                            sb.AppendFormat("<div class=\"{0}\">", CarcassMvcSettings.BootsrapFormFieldClass);
                        sb.Append(label.ToHtmlString());
                        if (isHorisontalForm)
                            sb.AppendFormat("<div class=\"{0}\">", CarcassMvcSettings.BootsrapFormFieldControlsClass);
                    }

                    // Reset ViewBag values
                    if (containerData.ContainsKey(itemFieldName))
                        containerData.Remove(itemFieldName);

                    var controlAttributes = editorAttributes.Clone();

                    var fullHtmlFieldName = templateInfo.GetFullHtmlFieldName(itemFieldName);
                    var validationAttributes = html.GetUnobtrusiveValidationAttributes(itemFieldName, metadata);
                    formContext.RenderedField(fullHtmlFieldName, false);

                    foreach (var attr in validationAttributes)
                    {
                        if(!controlAttributes.ContainsKey(attr.Key))
                            controlAttributes.Add(attr);
                    }

                    sb.Append("\r\n");
                    sb.Append(html.RenderCarcassEditor(metadata, null, itemFieldName, controlAttributes).ToHtmlString());
                    sb.Append("\r\n");

                    if (!isHidden && !metadata.HideSurroundingHtml)
                    {
                        if (inlineValidation)
                        {
                            var typeName = metadata.ModelType.Name;
                            if (metadata.IsNullableValueType && metadata.ModelType.GenericTypeArguments.Length > 0)
                                typeName = metadata.ModelType.GenericTypeArguments[0].Name;

                            sb.Append(" ");
                            if (typeName == "DateTime")
                            {
                                sb.Append(
                                    ValidationExtensions.FieldValidationMessage(html, metadata, itemFieldName + DateControlPostfix, null, validationAttrs.Clone())
                                        .ToHtmlString()).Append(" ");
                                sb.Append(
                                    ValidationExtensions.FieldValidationMessage(html, metadata, itemFieldName + TimeControlPostfix, null, validationAttrs.Clone())
                                        .ToHtmlString()).Append(" ");
//.........这里部分代码省略.........
开发者ID:kamaelyoung,项目名称:Carcass,代码行数:101,代码来源:EditorTemplates.cs

示例10: RenderReadOnly

        public static string RenderReadOnly(HtmlHelper html, BootstrapReadOnlyModel model, bool isPassword)
        {
            var combinedHtml = "{0}{1}{2}";

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

            if (!string.IsNullOrEmpty(model.id)) model.htmlAttributes.Add("id", model.id);
            if (model.tooltipConfiguration != null) model.htmlAttributes.MergeHtmlAttributes(model.tooltipConfiguration.ToDictionary());
            if (model.tooltip != null) model.htmlAttributes.MergeHtmlAttributes(model.tooltip.ToDictionary());
            // assign placeholder class
            if (!string.IsNullOrEmpty(model.placeholder)) model.htmlAttributes.Add("placeholder", model.placeholder);
            // build html for input

            string input = html.Hidden(model.htmlFieldName, model.value).ToHtmlString();

            if (model.value != null && model.value.GetType().IsEnum)
            {
                input =  input + ((Enum)model.value).GetEnumDescription();
            }
            else
            {
                input = input + html.Encode(model.value);

            }

            // account for appendString, prependString, and AppendButtons
            if (!string.IsNullOrEmpty(model.prependString) ||
                !string.IsNullOrEmpty(model.appendString) ||
                model.prependButtons.Any() ||
                model.appendButtons.Any() ||
                model.iconPrepend != Icons._not_set ||
                model.iconAppend != Icons._not_set ||
                !string.IsNullOrEmpty(model.iconPrependCustomClass) ||
                !string.IsNullOrEmpty(model.iconAppendCustomClass))
            {
                var appendPrependContainer = new TagBuilder("div");
                var addOnPrependString = "";
                var addOnAppendString = "";
                var addOnPrependButtons = "";
                var addOnAppendButtons = "";
                var addOnPrependIcon = "";
                var addOnAppendIcon = "";

                var 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.prependButtons.Count > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    model.prependButtons.ForEach(x => addOnPrependButtons += x.ToHtmlString());
                }
                if (model.appendButtons.Count > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    model.appendButtons.ForEach(x => addOnAppendButtons += x.ToHtmlString());
                }
                if (model.iconPrepend != Icons._not_set)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    addOn.InnerHtml = new BootstrapIcon(model.iconPrepend, model.iconPrependIsWhite).ToHtmlString();
                    addOnPrependIcon = addOn.ToString();
                }
                if (model.iconAppend != Icons._not_set)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    addOn.InnerHtml = new BootstrapIcon(model.iconAppend, model.iconAppendIsWhite).ToHtmlString();
                    addOnAppendIcon = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.iconPrependCustomClass))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    var i = new TagBuilder("i");
                    i.AddCssClass(model.iconPrependCustomClass);
                    addOn.InnerHtml = i.ToString(TagRenderMode.Normal);
                    addOnPrependIcon = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.iconAppendCustomClass))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    var i = new TagBuilder("i");
                    i.AddCssClass(model.iconAppendCustomClass);
                    addOn.InnerHtml = i.ToString(TagRenderMode.Normal);
                    addOnAppendIcon = addOn.ToString();
                }

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

//.........这里部分代码省略.........
开发者ID:ngadotnet,项目名称:TwitterBootstrapMvc,代码行数:101,代码来源:Renderer.ReadOnly.cs

示例11: RenderTextBox

        public static HtmlString RenderTextBox(HtmlHelper html, TextBoxModel model, bool isPassword)
        {
            var combinedHtml = "{0}{1}{2}";

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

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

            if (!string.IsNullOrEmpty(model.placeholder))
                model.htmlAttributes.Add("placeholder", model.placeholder);

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

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

            var input = isPassword
                ? html.Password(model.htmlFieldName, null, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString()
                : html.TextBox(model.htmlFieldName, model.value, model.format, model.htmlAttributes.FormatHtmlAttributes())
                    .ToHtmlString();

            // account for appendString, prependString, and AppendButtons
            if (!string.IsNullOrEmpty(model.prependString) || !string.IsNullOrEmpty(model.appendString) ||
                model.iconPrepend != Icons._not_set
                || model.iconAppend != Icons._not_set || !string.IsNullOrEmpty(model.iconPrependCustomClass) ||
                !string.IsNullOrEmpty(model.iconAppendCustomClass))
            {
                var appendPrependContainer = new TagBuilder("div");
                appendPrependContainer.AddOrMergeCssClass("input-group");
                appendPrependContainer.AddOrMergeCssClass("mar-btm");

                var addOnPrependString = "";
                var addOnAppendString = "";
                var addOnPrependIcon = "";
                var addOnAppendIcon = "";

                var addOn = new TagBuilder("span");
                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();
                }

                if (model.iconPrepend != Icons._not_set)
                {
                    addOn.InnerHtml = new Icon(model.iconPrepend, model.iconPrependIsWhite).ToHtmlString();
                    addOnPrependIcon = addOn.ToString();
                }

                if (model.iconAppend != Icons._not_set)
                {
                    addOn.InnerHtml = new Icon(model.iconAppend, model.iconAppendIsWhite).ToHtmlString();
                    addOnAppendIcon = addOn.ToString();
                }

                if (!string.IsNullOrEmpty(model.iconPrependCustomClass))
                {
                    var i = new TagBuilder("i");
                    i.AddCssClass(model.iconPrependCustomClass);
                    addOn.InnerHtml = i.ToString(TagRenderMode.Normal);
                    addOnPrependIcon = addOn.ToString();
                }

                if (!string.IsNullOrEmpty(model.iconAppendCustomClass))
                {
                    var i = new TagBuilder("i");
                    i.AddCssClass(model.iconAppendCustomClass);
                    addOn.InnerHtml = i.ToString(TagRenderMode.Normal);
                    addOnAppendIcon = addOn.ToString();
                }

                appendPrependContainer.InnerHtml = addOnPrependIcon + addOnPrependString + "{0}" + addOnAppendString +
                                                   addOnAppendIcon;
                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 htmlTextBox = string.Format(combinedHtml, input, helpText, validationMessage);

            TagBuilder inputWrapper = null;

            if (!string.IsNullOrEmpty(model.inputElementWrapper))
            {
//.........这里部分代码省略.........
开发者ID:andrewreyes,项目名称:NiftyMvcHelpers,代码行数:101,代码来源:RenderTextBox.cs

示例12: RenderTextBox

        public static string RenderTextBox(HtmlHelper html, BootstrapTextBoxModel model, bool isPassword)
        {
            if (model == null || string.IsNullOrEmpty(model.htmlFieldName)) return null;

            string combinedHtml = "{0}{1}{2}";

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

            if (!string.IsNullOrEmpty(model.id)) model.htmlAttributes.Add("id", model.id);
            if (model.tooltipConfiguration != null) model.htmlAttributes.MergeHtmlAttributes(model.tooltipConfiguration.ToDictionary());
            // assign placeholder class
            if (!string.IsNullOrEmpty(model.placeholder)) model.htmlAttributes.Add("placeholder", model.placeholder);
            // assign size class
            model.htmlAttributes.AddOrMergeCssClass("class", BootstrapHelper.GetClassForInputSize(model.size));
            // build html for input
            var input = isPassword
                ? html.Password(model.htmlFieldName, null, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString()
                : html.TextBox(model.htmlFieldName, model.value, model.format, model.htmlAttributes.FormatHtmlAttributes()).ToHtmlString();

            // account for appendString, prependString, and AppendButtons
            if (!string.IsNullOrEmpty(model.prependString) ||
                !string.IsNullOrEmpty(model.appendString) ||
                model.prependButtons.Any() ||
                model.appendButtons.Any() ||
                model.iconPrepend != Icons._not_set ||
                model.iconAppend != Icons._not_set ||
                !string.IsNullOrEmpty(model.iconPrependCustomClass) ||
                !string.IsNullOrEmpty(model.iconAppendCustomClass))
            {
                TagBuilder appendPrependContainer = new TagBuilder("div");
                string addOnPrependString = "";
                string addOnAppendString = "";
                string addOnPrependButtons = "";
                string addOnAppendButtons = "";
                string addOnPrependIcon = "";
                string addOnAppendIcon = "";

                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.prependButtons.Count() > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    model.prependButtons.ForEach(x => addOnPrependButtons += x.ToHtmlString());
                }
                if (model.appendButtons.Count() > 0)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    model.appendButtons.ForEach(x => addOnAppendButtons += x.ToHtmlString());
                }
                if (model.iconPrepend != Icons._not_set)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    addOn.InnerHtml = new BootstrapIcon(model.iconPrepend, model.iconPrependIsWhite).ToHtmlString();
                    addOnPrependIcon = addOn.ToString();
                }
                if (model.iconAppend != Icons._not_set)
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    addOn.InnerHtml = new BootstrapIcon(model.iconAppend, model.iconAppendIsWhite).ToHtmlString();
                    addOnAppendIcon = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.iconPrependCustomClass))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-prepend");
                    var i = new TagBuilder("i");
                    i.AddCssClass(model.iconPrependCustomClass);
                    addOn.InnerHtml = i.ToString(TagRenderMode.Normal);
                    addOnPrependIcon = addOn.ToString();
                }
                if (!string.IsNullOrEmpty(model.iconAppendCustomClass))
                {
                    appendPrependContainer.AddOrMergeCssClass("input-append");
                    var i = new TagBuilder("i");
                    i.AddCssClass(model.iconAppendCustomClass);
                    addOn.InnerHtml = i.ToString(TagRenderMode.Normal);
                    addOnAppendIcon = addOn.ToString();
                }

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

            string helpText = model.helpText != null ? model.helpText.ToHtmlString() : string.Empty;
            string validationMessage = "";
            if(model.displayValidationMessage)
            {
                string validation = html.ValidationMessage(model.htmlFieldName).ToHtmlString();
                validationMessage = new BootstrapHelpText(validation, model.validationMessageStyle).ToHtmlString();
//.........这里部分代码省略.........
开发者ID:Boggin,项目名称:TwitterBootstrapMvc,代码行数:101,代码来源:Renderer.TextBox.cs

示例13: RenderDatePicker

        public static HtmlString RenderDatePicker(HtmlHelper html, DatePickerModel model)
        {
            var combinedHtml = "{0}{1}{2}";
            var addOn = new TagBuilder("span");
            var container = new TagBuilder("div");
            var addOnPrependString = "";
            var addOnPrependIcon = "";
            var addOnAppendIcon = "";

            var htmlAttributes = new
            {
                @type = "text"
            }.ObjectToHtmlAttributesDictionary();

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

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

            if (!string.IsNullOrEmpty(model.placeholder))
                model.htmlAttributes.Add("placeholder", model.placeholder);

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

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

            if (!string.IsNullOrEmpty(model.dateFormat))
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.dateFormat), model.dateFormat);

            if (model.weekStart != 0)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.weekStart), model.weekStart.ToString());

            if(model.autoclose)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.autoclose), model.autoclose.ToString().ToLower());

            if(model.calendarWeeks)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.calendarWeeks), model.calendarWeeks.ToString().ToLower());

            if(model.clearBtn)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.clearBtn), model.clearBtn.ToString().ToLower());

            if(!string.IsNullOrEmpty(model.container))
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.container), model.container);

            if(model.datesDisabled.Any())
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.datesDisabled), new JavaScriptSerializer().Serialize(model.datesDisabled));

            if(model.daysOfWeekDisabled.Any())
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.daysOfWeekDisabled), new JavaScriptSerializer().Serialize(model.daysOfWeekDisabled));

            if (model.daysOfWeekHighlighted.Any())
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.daysOfWeekHighlighted), new JavaScriptSerializer().Serialize(model.daysOfWeekHighlighted));

            if (!string.IsNullOrEmpty(model.defaultViewDate))
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.defaultViewDate), model.defaultViewDate);

            if(model.disableTouchKeyboard)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.disableTouchKeyboard), model.disableTouchKeyboard.ToString().ToLower());

            if(!model.enableOnReadonly)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.enableOnReadonly), model.enableOnReadonly.ToString().ToLower());

            if (model.endDate != null)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.endDate), model.endDate.ToString());

            if(!model.forceParse)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.forceParse), model.forceParse.ToString().ToLower());

            if(model.immediateUpdates)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.immediateUpdates), model.immediateUpdates.ToString().ToLower());

            if (!model.keyboardNavigation)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.keyboardNavigation), model.keyboardNavigation.ToString().ToLower());

            if(!string.IsNullOrEmpty(model.language))
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.language), model.language);

            if(model.multidate)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.multidate), model.multidate.ToString().ToLower());

            if(!string.IsNullOrEmpty(model.multidateSeparator))
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.multidateSeparator), model.multidateSeparator);

            if(!string.IsNullOrEmpty(model.orientation))
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.orientation), model.orientation);

            if(!model.showOnFocus)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.showOnFocus), model.showOnFocus.ToString().ToLower());

            if(model.startDate != null)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.startDate), model.startDate.ToString());

            if(model.startView != 0)
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.startView), model.startView.ToString());

            if(!string.IsNullOrEmpty(model.title))
                model.htmlAttributes.AddOrReplace(model.HtmlAttr(() => model.title), model.title);

//.........这里部分代码省略.........
开发者ID:andrewreyes,项目名称:NiftyMvcHelpers,代码行数:101,代码来源:RenderDatePicker.cs

示例14: ChildValidationAttributes

        public static IList<KeyValuePair<string, object>> ChildValidationAttributes(HtmlHelper htmlHelper, string basePropertyName, string targetPropertyName, ModelMetadata metadata)
        {
            var attributeKeyPropertyPath = targetPropertyName.ToLower().Replace(".", "");

            IDictionary<string, object> validationAttributes;
            if (StoredValidationAttributes.Any(q => q.PropertyName == basePropertyName && q.Metadata == metadata))
            {
                validationAttributes = StoredValidationAttributes.First(q => q.PropertyName == basePropertyName && q.Metadata == metadata).Attributes;
            }
            else
            {
                validationAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(basePropertyName, metadata);
                StoredValidationAttributes.Add(new UiDateHelperValidationAttribute {PropertyName = basePropertyName, Metadata = metadata, Attributes = validationAttributes});
            }
            var associatedValidationAttributes = validationAttributes.Where(attr => attr.Key.EndsWith(attributeKeyPropertyPath) || attr.Key.Contains(attributeKeyPropertyPath + "-")).ToList();
            if (associatedValidationAttributes.Any())
            {
                var dataValAttribute = validationAttributes.FirstOrDefault(attr => attr.Key == "data-val");
                associatedValidationAttributes.Add(dataValAttribute);
            }

            return associatedValidationAttributes;
        }
开发者ID:rantoine,项目名称:CSharpMVCDateTimeConversionFramework,代码行数:23,代码来源:UiDateTimeUtilities.cs

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