代码之家  ›  专栏  ›  技术社区  ›  Shaun Luttin

在作为标记助手调用的视图组件中,我们如何访问内部HTML?

  •  1
  • Shaun Luttin  · 技术社区  · 7 年前

    <!-- Index.cshtml -->  
    <my-first>
        This is the original Inner HTML from the *.cshtml file.
    </my-first>
    
    // MyFirstTagHelper.cs > ProcessAsync
    var childContent = await output.GetChildContentAsync();
    var innerHtml = childContent.GetContent();
    

    invoke a view component as a tag helper

    <vc:my-first>
        This is the original Inner HTML from the *.cshtml file.
    </vc:my-first>
    
    // MyFirstViewComponent.cs > InvokeAsync()
    var innerHtml = DoMagic();
    

    进一步思考:

    有些人可能会说,“那为什么不使用标签助手呢?”好吧,我们希望将视图与标记助手相关联,而标记助手不支持视图;相反,标记助手要求我们以编程方式构建所有HTML。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Adam Vincent    7 年前

    抱歉,答案是“只使用标签助手”

    https://github.com/aspnet/Mvc/issues/5465

    ViewComponent @Component.Invoke() .

    推荐文章