I have the following tag helper (this is the equivalent of saying Html.Editor):
[HtmlTargetElement("editor", Attributes = "for", TagStructure = TagStructure.WithoutEndTag)]
public class EditorTagHelper : TagHelper {
private readonly IHtmlHelper _htmlHelper;
public EditorTagHelper(IHtmlHelper htmlHelper) {
_htmlHelper = htmlHelper;
}
public ModelExpression For { get; set; }
public IDictionary<string, string> HtmlAttributes { get; set; } = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
public string TemplateName { get; set; }
public IDictionary<string, object> ViewData { get; set; } = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
[HtmlAttributeNotBound, ViewContext]
public ViewContext ViewContext { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output) {
((IViewContextAware)_htmlHelper).Contextualize(ViewContext);
ViewData.Add("HtmlAttributes", HtmlAttributes);
output.Content.SetHtmlContent(_htmlHelper.Editor(For.Name, TemplateName, ViewData));
output.TagName = null;
}
}
Which is called like so:
<editor for="Name" view-data-test="@("Foo")" html-attributes-class="Bar" />
Here's the code for the String view template:
@model string
<input asp-for="@Model" class="form-control" />
@ViewData["Test"]
@(((IDictionary<string, string>)ViewData["HtmlAttributes"])["Class"])
This works fine but ideally I'd like to add the HtmlAttributes dictionary as attributes to the input tag helper above. Previously I would have said the following to pass the attributes to a HTML helper:
Html.TextBoxFor(m => m, new { htmlAttributes = ViewData["HtmlAttributes"] })
But what is the equivalent to pass them to a tag helper?