【发布时间】:2019-12-27 18:00:38
【问题描述】:
我有以下标签助手(这相当于说 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;
}
}
这样称呼:
<editor for="Name" view-data-test="@("Foo")" html-attributes-class="Bar" />
这是字符串视图模板的代码:
@model string
<input asp-for="@Model" class="form-control" />
@ViewData["Test"]
@(((IDictionary<string, string>)ViewData["HtmlAttributes"])["Class"])
这很好用,但理想情况下,我想将 HtmlAttributes 字典作为属性添加到上面的输入标签助手中。以前我会说以下将属性传递给 HTML 助手:
Html.TextBoxFor(m => m, new { htmlAttributes = ViewData["HtmlAttributes"] })
但是将它们传递给标签助手的等价物是什么?
【问题讨论】:
标签: c# asp.net-core razor asp.net-core-tag-helpers