创建要在视图中使用的 method/function
Create a method/function to use within a view
我们如何编写函数以在 *.cshtml
页面中使用。我们过去可以在视图中使用 @helper
或 @function
。我们如何做到这一点?例如,我想编写一个递归函数来显示所有配置值。我该怎么做?
<dl>
@foreach(var k in config.GetSubKeys())
{
<dt>@k.Key</dt>
<dd>@config.Get(k.Key)</dd>
@* TODO How can we make this a helper function/recursive? *@
@foreach(var sk in config.GetSubKey(k.Key).GetSubKeys())
{
<dt>@sk.Key</dt>
<dd>@config.Get(sk.Key)</dd>
}
}
</dl>
我想我们需要在 project.json 中添加一个依赖项,然后在 Startup.cs 中选择使用它。
指的是few design discussions that we only have glimpses of online, @helper
was removed for design reasons; the replacement is View Components.
我推荐如下所示的视图组件:
public class ConfigurationKeysViewComponent : ViewComponent
{
private readonly IConfiguration config;
public ConfigurationKeysViewComponent(IConfiguration config)
{
this.config = config;
}
public IViewComponentResult Invoke(string currentSubKey = "")
{
return View(new ConfigurationData
{
Key = currentSubKey,
Value = config.Get(currentSubKey),
SubKeys = config.GetSubKey(currentSubKey).GetSubKeys().Select(sk => sk.Key)
});
}
}
您的 ViewComponent 的视图将相对简单:
<dt>@Model.Key</dt>
<dd>@config.Get(Model.Key)</dd>
@foreach (var sk in Model.SubKeys)
{
@Component.Invoke("ConfigurationKeys", sk)
}
然后您可以从根视图调用它,如下所示:
@Component.Invoke("ConfigurationKeys")
免责声明:我是在SO编辑器中写的,可能存在编译错误。此外,我不确定视图组件是否支持默认参数 - 您可能需要在根视图对视图组件的调用中添加默认值 ""
。
或者,如果这只是调试代码,您可以使用 Stack<>
.
展开递归
假设您的视图组件提供递归模型,使用 razor 视图快速而肮脏。
组件视图:
@model YourRecursiveDataStructure
<ul class="sidebar-menu">
<li class="header">MAIN NAVIGATION</li>
@foreach (var node in Model.RootNodes)
{
@Html.Partial("~/YourPath/RenderElement.cshtml", node)
}
</ul>
在视图中呈现元素:
@model YourRecursiveNode
<li>
<a href="@Model.Href">
<span>@Model.Display</span>
</a>
@Html.Partial("~/YourPath/RenderChildren.cshtml", Model)
</li>
然后在另一个视图中循环节点的子节点:
@model YourRecursiveNode
@if (Model.HasChildren)
{
<ul>
@foreach (var child in Model.Children)
{
@Html.Partial("~/YourPath/RenderElement.cshtml", child)
}
</ul>
}
我们如何编写函数以在 *.cshtml
页面中使用。我们过去可以在视图中使用 @helper
或 @function
。我们如何做到这一点?例如,我想编写一个递归函数来显示所有配置值。我该怎么做?
<dl>
@foreach(var k in config.GetSubKeys())
{
<dt>@k.Key</dt>
<dd>@config.Get(k.Key)</dd>
@* TODO How can we make this a helper function/recursive? *@
@foreach(var sk in config.GetSubKey(k.Key).GetSubKeys())
{
<dt>@sk.Key</dt>
<dd>@config.Get(sk.Key)</dd>
}
}
</dl>
我想我们需要在 project.json 中添加一个依赖项,然后在 Startup.cs 中选择使用它。
指的是few design discussions that we only have glimpses of online, @helper
was removed for design reasons; the replacement is View Components.
我推荐如下所示的视图组件:
public class ConfigurationKeysViewComponent : ViewComponent
{
private readonly IConfiguration config;
public ConfigurationKeysViewComponent(IConfiguration config)
{
this.config = config;
}
public IViewComponentResult Invoke(string currentSubKey = "")
{
return View(new ConfigurationData
{
Key = currentSubKey,
Value = config.Get(currentSubKey),
SubKeys = config.GetSubKey(currentSubKey).GetSubKeys().Select(sk => sk.Key)
});
}
}
您的 ViewComponent 的视图将相对简单:
<dt>@Model.Key</dt>
<dd>@config.Get(Model.Key)</dd>
@foreach (var sk in Model.SubKeys)
{
@Component.Invoke("ConfigurationKeys", sk)
}
然后您可以从根视图调用它,如下所示:
@Component.Invoke("ConfigurationKeys")
免责声明:我是在SO编辑器中写的,可能存在编译错误。此外,我不确定视图组件是否支持默认参数 - 您可能需要在根视图对视图组件的调用中添加默认值 ""
。
或者,如果这只是调试代码,您可以使用 Stack<>
.
假设您的视图组件提供递归模型,使用 razor 视图快速而肮脏。
组件视图:
@model YourRecursiveDataStructure
<ul class="sidebar-menu">
<li class="header">MAIN NAVIGATION</li>
@foreach (var node in Model.RootNodes)
{
@Html.Partial("~/YourPath/RenderElement.cshtml", node)
}
</ul>
在视图中呈现元素:
@model YourRecursiveNode
<li>
<a href="@Model.Href">
<span>@Model.Display</span>
</a>
@Html.Partial("~/YourPath/RenderChildren.cshtml", Model)
</li>
然后在另一个视图中循环节点的子节点:
@model YourRecursiveNode
@if (Model.HasChildren)
{
<ul>
@foreach (var child in Model.Children)
{
@Html.Partial("~/YourPath/RenderElement.cshtml", child)
}
</ul>
}