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(); }
public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output) { var childContent = output.TagMode == TagMode.StartTagAndEndTag ? await output.GetChildContentAsync() : null; if (childContent == null && AspFor == null) { throw new InvalidOperationException($"Cannot determine content."); } var resolvedContent = (IHtmlContent)childContent; if (resolvedContent == null && AspFor != null) { var validationMessage = _modelHelper.GetValidationMessage( ViewContext, AspFor.ModelExplorer, AspFor.Name); if (validationMessage != null) { resolvedContent = new HtmlString(validationMessage); } } if (resolvedContent != null) { var tagBuilder = _htmlGenerator.GenerateErrorMessage( VisuallyHiddenText, Id, resolvedContent, output.Attributes.ToAttributesDictionary()); output.TagName = tagBuilder.TagName; output.TagMode = TagMode.StartTagAndEndTag; output.Attributes.Clear(); output.MergeAttributes(tagBuilder); output.Content.SetHtmlContent(tagBuilder.InnerHtml); } else { output.SuppressOutput(); } }