如何访问 ASP.NET CORE ViewComponent 中标签内的内容?
How can I access the content within the tags in an ASP.NET CORE ViewComponent?
我创建了一个在 Razor 页面上呈现一些 HTML 的 ViewComponent。看起来像这样:
<vc:Blockheader button-text="Create new skill" block-title="@ViewData["Title"]">
hello
</vc:Blockheader>
如何从 ViewComponent class 访问值 hello?
您无权访问此内容。视图组件的参数作为属性传递(在小写的 kebab 中),因此您可以向 Invoke
方法添加一个额外的参数。
示例:
public class BlockHeader : ViewComponent
{
public IViewComponentResult Invoke(string buttonText, string blockTitle, string message)
{
// ...
return View<string>(message);
}
}
调用视图组件作为标签助手:
<vc:block-header button-text="param1Value" block-title="param2Value" message="hello">
</vc:block-header>
我创建了一个在 Razor 页面上呈现一些 HTML 的 ViewComponent。看起来像这样:
<vc:Blockheader button-text="Create new skill" block-title="@ViewData["Title"]">
hello
</vc:Blockheader>
如何从 ViewComponent class 访问值 hello?
您无权访问此内容。视图组件的参数作为属性传递(在小写的 kebab 中),因此您可以向 Invoke
方法添加一个额外的参数。
示例:
public class BlockHeader : ViewComponent
{
public IViewComponentResult Invoke(string buttonText, string blockTitle, string message)
{
// ...
return View<string>(message);
}
}
调用视图组件作为标签助手:
<vc:block-header button-text="param1Value" block-title="param2Value" message="hello">
</vc:block-header>