【问题标题】:How can I access the content within the tags in an ASP.NET CORE ViewComponent?如何访问 ASP.NET CORE ViewComponent 中标签内的内容?
【发布时间】:2019-01-09 09:21:24
【问题描述】:
我创建了一个在 Razor 页面上呈现一些 HTML 的 ViewComponent。看起来像这样:
<vc:Blockheader button-text="Create new skill" block-title="@ViewData["Title"]">
hello
</vc:Blockheader>
如何从 ViewComponent 类中访问 hello 值?
【问题讨论】:
标签:
c#
asp.net-core
razor-pages
asp.net-core-viewcomponent
【解决方案1】:
您无权访问此内容。视图组件的参数作为属性传递(在小写的 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>