用于ViewComponent渲染的TagHelper

时间:2015-11-16 20:34:18

标签: asp.net-core asp.net-core-tag-helpers asp.net-core-viewcomponent

在重新发明轮子之前(在这种情况下不确定是否可能!),是否有一段代码显示如何在Tag Helper实现中调用ViewComponent?

编辑:包含来自泰勒talk

的代码
private readonly IViewComponentHelper _viewComponentHelper;
public ShapeishTagHelper(IViewComponentHelper viewComponentHelper) {
    _viewComponentHelper = viewComponentHelper;
}

[ViewContext]
public ViewContext ViewContext { get; set; }

public string Name { get; set; }
public int Age { get; set }

public override void Process(TagHelperContext, TagHelperOutput output) {
    output.TagName = null;
    ((ICanHasViewContext)_viewComponentHelper).Contextualize(ViewContext);
    var viewContent = _viewComponentHelper.Invoke("Shapeish", Name, Age);
    output.Content.SetContent(viewContent);
}

0 个答案:

没有答案