Ejemplo n.º 1
0
        private static MvcHtmlString LabelHelper(HtmlHelper html,
                                                 ModelMetadata metadata, string htmlFieldName, string labelText)
        {
            if (string.IsNullOrEmpty(labelText))
            {
                labelText = metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();
            }

            if (string.IsNullOrEmpty(labelText))
            {
                return(MvcHtmlString.Empty);
            }

            bool isRequired = false;

            if (metadata.ContainerType != null)
            {
                isRequired = metadata.ContainerType.GetProperty(metadata.PropertyName)
                             .GetCustomAttributes(typeof(RequiredAttribute), false)
                             .Length == 1;
            }

            var tag = new TagBuilder("label");

            tag.Attributes.Add(
                "for",
                TagBuilder.CreateSanitizedId(
                    html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)
                    )
                );

            if (isRequired)
            {
                tag.Attributes.Add("class", "label-required");
            }

            tag.SetInnerText(labelText);

            var output = tag.ToString(TagRenderMode.Normal);


            if (isRequired)
            {
                var asteriskTag = new TagBuilder("span");
                asteriskTag.Attributes.Add("class", "required");
                asteriskTag.SetInnerText("*");
                output += asteriskTag.ToString(TagRenderMode.Normal);
            }
            return(MvcHtmlString.Create(output));
        }
Ejemplo n.º 2
0
        public DynamicComponentBase(HtmlHelper <TModel> helper, string fieldId, string name, string type, object viewData = null, bool nullable = false, bool readOnly = false, bool disabled = false, bool visible = true)
        {
            FieldName     = name;
            FieldFullName = helper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(FieldName);
            SanitizedId   = TagBuilder.CreateSanitizedId(FieldFullName);

            RouteValueDictionary viewDataObj = HtmlHelper.AnonymousObjectToHtmlAttributes(viewData);

            HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(viewDataObj["htmlAttributes"]);
            HtmlAttributes = (RouteValueDictionary)helper.MergeHtmlAttributes(HtmlAttributes, viewDataObj);

            FieldIsNullable = nullable;
            FieldIsReadOnly = readOnly;
            FieldIsDisabled = disabled;
            FieldIsVisible  = visible;
        }
Ejemplo n.º 3
0
        static MvcHtmlString FormItemLabel(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText)
        {
            string str = labelText ?? (metadata.DisplayName ?? (metadata.PropertyName ?? htmlFieldName.Split(new char[] { '.' }).Last <string>()));

            if (string.IsNullOrEmpty(str))
            {
                return(MvcHtmlString.Empty);
            }
            TagBuilder tagBuilder = new TagBuilder("label");

            tagBuilder.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
            tagBuilder.Attributes.Add("class", "ui-form-item-label");
            tagBuilder.SetInnerText(str);

            return(new MvcHtmlString(tagBuilder.ToString(TagRenderMode.Normal)));
        }
Ejemplo n.º 4
0
        private static BootstrapLabel BeginLabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string className, string labelText = null)
        {
            string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();

            TagBuilder tag = new TagBuilder("label");

            tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
            tag.Attributes.Add("class", className);
            tag.SetInnerText(resolvedLabelText);

            html.ViewContext.Writer.Write(tag.ToString(TagRenderMode.StartTag));

            BootstrapLabel theLabel = new BootstrapLabel(html.ViewContext, resolvedLabelText);

            return(theLabel);
        }
Ejemplo n.º 5
0
        internal static MvcHtmlString LabelHelper(HtmlHelper html, ModelMetadata metadata, string htmlFieldName, string labelText = null)
        {
            string resolvedLabelText = labelText ?? metadata.DisplayName ?? metadata.PropertyName ?? htmlFieldName.Split('.').Last();

            if (String.IsNullOrEmpty(resolvedLabelText))
            {
                return(MvcHtmlString.Empty);
            }

            TagBuilder tag = new TagBuilder("label");

            tag.Attributes.Add("for", TagBuilder.CreateSanitizedId(html.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(htmlFieldName)));
            tag.Attributes.Add("class", "control-label");
            tag.SetInnerText(resolvedLabelText);
            return(new MvcHtmlString(tag.ToString(TagRenderMode.Normal)));
        }
        public static string GenerateIdFromName(string name, string idAttributeDotReplacement)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            if (idAttributeDotReplacement == null)
            {
                throw new ArgumentNullException("idAttributeDotReplacement");
            }

            // TagBuilder.CreateSanitizedId returns null for empty strings, return String.Empty instead to avoid breaking change
            if (name.Length == 0)
            {
                return(String.Empty);
            }

            return(TagBuilder.CreateSanitizedId(name, idAttributeDotReplacement));
        }
Ejemplo n.º 7
0
 /// <summary>Replaces each invalid character in the tag ID with a valid HTML character.</summary>
 /// <returns>The sanitized tag ID, or null if <paramref name="originalId" /> is null or empty, or if <paramref name="originalId" /> does not begin with a letter.</returns>
 /// <param name="originalId">The ID that might contain characters to replace.</param>
 public static string CreateSanitizedId(string originalId)
 {
     return(TagBuilder.CreateSanitizedId(originalId, HtmlHelper.IdAttributeDotReplacement));
 }