您可以在@Helper中使用@Helper吗? [英] Can you use a @Helper inside an @Helper?
问题描述
我不确定这是否可能.
在一个视图以及其他视图中,我有一堆 @Helper
:
I have a bunch of @Helper
's inside a view AND in other views:
@helper ViewHelper1()
{
...
}
@helper ViewHelper2()
{
...
}
etc.
我在视图和其他视图中使用了重复的代码:
I have repetitive code that is used in the view AND in other views:
@if (!(Model.Entity == Model.Enum.One))
{
<td>
@ViewHelper1()
</td>
}
else
{
<td>
@ViewHelper1()
</td>
<td>
@ViewHelper1()
</td>
}
实际的 @ ViewHelper1
具有更复杂的代码,但这并不重要(我认为).
The actual @ViewHelper1
has more complex code, but that's not important (I think).
好吧,因为每个视图都有许多 @Helper
(30+个视图,每个视图有10-15个 @Helper
)和< table>
的结构是相同的,我想知道如何在 App_Code
中创建一个 @Helper
来封装< td>
结构,然后将其传递给视图的 @Helper
.
Well, since each view has a number of @Helper
's (30+ views, 10-15 @Helper
's each) and the <table>
structure is the same, I was wondering how to go about creating a @Helper
in App_Code
that encapsulates the <td>
structure and then would pass the view's @Helper
.
说:
@helper Table(...)
{
...
}
或者是否有可能,然后在如下视图中调用它:
Or whether or not that's even possible and then call it in the view like:
@Table(HelperView1)
如果是这样,我只需要语法方面的帮助.
If it is I just needed help with the syntax.
一如既往,非常感谢.
推荐答案
生成的剃刀助手只是返回类型为 HelperResult
的函数.您可以在主帮助器中具有将 HelperResult
作为参数返回的委托,并在适当的位置调用它们.
The generated razor helpers are just functions with the return type HelperResult
.
You can have delegates which returns HelperResult
as parameters in your main helper and call them at the appropriate places.
帮助您入门的小样本:
@helper View1()
{
<h1>View1</h1>
}
@helper View2()
{
<h2>View2</h2>
}
@helper Table(Func<HelperResult> viewHelper)
{
<text>Reuslt of viewHelper</text>
@viewHelper()
}
@Table(View1)
@Table(View2)
生成的输出:
Reuslt of viewHelper
<h1>View1</h1>
Reuslt of viewHelper
<h2>View2</h2>
这篇关于您可以在@Helper中使用@Helper吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!