ViewComponents with children

六月ゝ 毕业季﹏ 提交于 2020-05-30 12:59:27

问题


Can I use ViewComponents in a page like this.

<vc:parent-component>
     <some random html>
</parent-component>

when I try this it renders out only parent component. In cshtml of parent-component I am looking for something like below

<h2>Parent component markup</h2>
@RenderChildren()??
<h2>Parent component markup end</h2> 

So result will be

<h2>Parent component markup</h2>
@RenderChildren()
<h2>Parent component markup end</h2>

回答1:


You cannot use random html inside ViewComponent tag helper as you did. May be your are considering ViewComponent tag helper as like as html element. Actually its not html element, rather its a razor code.

According to the Invoking a view component as a Tag Helper documentation, Your ViewComponent tag helper should be like as follows

<vc:[view-component-name]
  parameter1="parameter1 value"
  parameter2="parameter2 value">
</vc:[view-component-name]>


来源:https://stackoverflow.com/questions/55206787/viewcomponents-with-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!