Beispiel #1
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (For == null && AspFor == null)
            {
                throw new InvalidOperationException($"Cannot determine 'for' attribute for <label>.");
            }

            var childContent = output.TagMode == TagMode.StartTagAndEndTag ?
                               await output.GetChildContentAsync() :
                               null;

            var resolvedFor = For ??
                              TagBuilder.CreateSanitizedId(
                _modelHelper.GetFullHtmlFieldName(ViewContext, AspFor.Name), Constants.IdAttributeDotReplacement);

            var resolvedContent = (IHtmlContent)childContent ??
                                  new HtmlString(_modelHelper.GetDisplayName(ViewContext, AspFor.ModelExplorer, AspFor.Name));

            var tagBuilder = _htmlGenerator.GenerateLabel(
                resolvedFor,
                IsPageHeading,
                resolvedContent,
                output.Attributes.ToAttributesDictionary());

            output.TagName = tagBuilder.TagName;
            output.TagMode = TagMode.StartTagAndEndTag;

            output.Attributes.Clear();
            output.MergeAttributes(tagBuilder);
            output.Content.SetHtmlContent(tagBuilder.InnerHtml);
        }
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            if (output.TagMode == TagMode.SelfClosing && AspFor == null)
            {
                throw new InvalidOperationException(
                          $"Content is required when the '{AspForAttributeName}' attribute is not specified.");
            }

            var errorSummaryContext = (ErrorSummaryContext)context.Items[typeof(ErrorSummaryContext)];

            var childContent = await output.GetChildContentAsync();

            IHtmlContent itemContent;

            if (output.TagMode == TagMode.StartTagAndEndTag)
            {
                itemContent = childContent.Snapshot();
            }
            else
            {
                var validationMessage = _modelHelper.GetValidationMessage(
                    ViewContext,
                    AspFor.ModelExplorer,
                    AspFor.Name);

                if (validationMessage == null)
                {
                    return;
                }

                itemContent = new HtmlString(validationMessage);
            }

            // If there are link attributes or AspFor specified then wrap content in a link
            if (For != null || AspFor != null)
            {
                var resolvedHref = For != null ?
                                   "#" + For :
                                   "#" + TagBuilder.CreateSanitizedId(
                    _modelHelper.GetFullHtmlFieldName(ViewContext, AspFor.Name),
                    Constants.IdAttributeDotReplacement);

                var link = new TagBuilder("a");
                link.Attributes.Add("href", resolvedHref);
                link.InnerHtml.AppendHtml(itemContent);

                itemContent = link;
            }

            errorSummaryContext.AddItem(new ErrorSummaryItem()
            {
                Content    = itemContent,
                Attributes = output.Attributes.ToAttributesDictionary()
            });

            output.SuppressOutput();
        }