[英]How to pass in a lambda to a Razor helper method?
I have a razor helper method that needs to take in a Func<>
that will return some HTML content to print out. 我有一个剃须刀助手方法需要接受一个
Func<>
,它将返回一些HTML内容打印出来。 This is what I originally had: 这是我原来的:
@helper node(string title, Func<HelperResult> descriptions)
{
....
<div>@descriptions()</div>
....
}
@node("title",
new Func<HelperResult>(() =>
{
return new HelperResult(
@<text>
<span>"desc1"</span>
<span>"desc2"</span>
</text>);
}))
Unfortunately with this my text never gets printed out. 不幸的是,我的文字永远不会打印出来。 No error either.
也没有错误。
So I learned about inline helpers, and changed the calling method to this: 所以我学习了内联帮助器,并将调用方法更改为:
@node("title",
@<text>
<span>"desc1"</span>
<span>"desc2"</span>
</text>)
However now I get a compilation error saying 但是现在我收到编译错误说
"Delegate 'System.Func' does not take 1 arguments".
“委托'System.Func'不接受1个参数”。
But I'm not passing in any arguments. 但我并没有传递任何论点。
So if I change it to Func<object,HelperResult>
and then call it using @descriptions(null)
I get the following error: 因此,如果我将其更改为
Func<object,HelperResult>
然后使用@descriptions(null)
调用它,我会收到以下错误:
"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"
“如果没有先将lambda表达式转换为委托或表达式树类型,则不能将lambda表达式用作动态调度操作的参数”
I'm sure I have something wrong somewhere, but I'm not sure what it is. 我确定我在某处出了点问题,但我不确定它到底是什么。
Edit : I think I may have solved that problem but it introduces some other issues. 编辑 :我想我可能已经解决了这个问题,但它引入了一些其他问题。
What I did was to cast the lambda before passing into a dynamic method. 我做的是在传递动态方法之前施放lambda。 I guess that's what the error was trying to say:
我猜这就是错误试图说的:
@node("title",
((Func<dynamic, HelperResult>)(@<text>
<span>"desc1"</span>
<span>"desc2"</span>
</text>))
That works and it prints out the span
tags correctly. 这有效,它可以正确地打印出
span
标签。 Unfortunately I have to pass in a useless parameter when calling this Func
. 不幸的是,我必须在调用此
Func
时传入一个无用的参数。
Now the issue I have is that my real function does a bit more than just write some spans. 现在我的问题是我的真正功能不仅仅是写一些跨度。 It's more like this:
它更像是这样的:
@node("title",
((Func<dynamic, HelperResult>)(@<text>
<span>@Helpers.Format(resource.Description,"item")</span>
</text>))
Where @Helpers.Format
is another helper and resource is a (dynamic) variable from the page model. 其中
@Helpers.Format
是另一个帮助器,资源是页面模型中的(动态)变量。
Of course now the code runs but nothing is printed out (inside the <span>
tag). 当然现在代码运行但没有打印出来(在
<span>
标签内)。 I put a breakpoint inside my Format
helper function, and it hits it and all the parameters are correctly set, so I'm not sure why it wouldn't output correctly. 我在我的
Format
帮助函数中放了一个断点,它命中它并且所有参数都被正确设置,所以我不确定它为什么输出不正确。 Similarly if I just change it to resource.Description then nothing still gets output. 同样,如果我只是将其更改为resource.Description,那么仍然没有输出。
Since it works well outside of this context, I wonder does Razor's inline helpers not capture the outer variables? 由于它在这个上下文之外运行良好,我想知道Razor的内联助手不能捕获外部变量吗?
Actually HelperResult is something Microsoft would rather you didn't use, as evidenced by documentation: 实际上HelperResult是微软宁愿你没有使用的东西,正如文档所证明的那样:
public class HelperResult : IHtmlString in namespace System.Web.WebPages
公共类HelperResult:名称空间System.Web.WebPages中的IHtmlString
Summary: This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
简介:此类型/成员支持.NET Framework基础结构,不能直接在您的代码中使用。
A possible solution to your problem might be to wrap your description function in another helper and then pass that helper as a method group to your node helper, like this: 您的问题的一个可能的解决方案可能是将您的描述函数包装在另一个帮助器中,然后将该帮助器作为方法组传递给您的节点帮助器,如下所示:
@helper Node(string title, Func<HelperResult> descriptions)
{
<div>@descriptions()</div>
}
@helper Description() {
<span>desc1</span>
<span>desc2</span>
}
@Node("title", Description)
In any case, your first idea shouldn't work because a parameter of type Func is in fact equal to a parameterless function, in which case you need to write the lambda expression like this: 在任何情况下,你的第一个想法都不应该工作,因为Func类型的参数实际上等于无参数函数,在这种情况下你需要像这样编写lambda表达式:
myFunction( () => doSomething)
So your function call would have been: 所以你的函数调用应该是:
@node("title", () =>
@<text>
<span>"desc1"</span>
<span>"desc2"</span>
</text>)
Since the future of these helpers is a bit dubious though, I would consider switching to either HtmlHelpers for small snippets of html or Partials for larger chunks. 由于这些助手的未来有点可疑 ,我会考虑切换到HtmlHelpers用于html的小片段或用于较大块的Partials 。
@Test(new Func<object, HelperResult>[]{@<text>hello</text>})
@Test(new Func<object, HelperResult>[]{@<text>hello</text>,@<text>world</text>})
@helper Test(params Func<object, HelperResult>[] results)
{
foreach (var result in results)
{
@result(null);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.