如何将lambda传递给Razor助手方法?
我有一个需要使用Func<>
的剃刀助手方法,该方法将返回一些HTML内容以进行打印.这就是我最初的经历:
I have a razor helper method that needs to take in a Func<>
that will return some HTML content to print out. 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'不 接受1个参数".
"Delegate 'System.Func' does not take 1 arguments".
但是我没有传递任何参数.
But I'm not passing in any arguments.
因此,如果将其更改为Func<object,HelperResult>
,然后使用@descriptions(null)
进行调用,则会出现以下错误:
So if I change it to Func<object,HelperResult>
and then call it using @descriptions(null)
I get the following error:
不能将lambda表达式用作动态 调度的操作,而无需先将其强制委托给委托人或 表达式树类型"
"Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type"
我确定某个地方出了点问题,但是我不确定那是什么.
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.
我要做的是在传递给动态方法之前先转换lambda.我想这就是错误要说的:
What I did was to cast the lambda before passing into a dynamic method. I guess that's what the error was trying to say:
@node("title",
((Func<dynamic, HelperResult>)(@<text>
<span>"desc1"</span>
<span>"desc2"</span>
</text>))
这有效,并且可以正确打印出span
标签.不幸的是,调用此Func
时,我必须传递一个无用的参数.
That works and it prints out the span
tags correctly. Unfortunately I have to pass in a useless parameter when calling this 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>))
其中@Helpers.Format
是另一个帮助器,而资源是页面模型中的(动态)变量.
Where @Helpers.Format
is another helper and resource is a (dynamic) variable from the page model.
当然,现在代码可以运行,但是什么都没有打印出来(在<span>
标记内).我在Format
辅助函数中放置了一个断点,命中了断点,并且所有参数都已正确设置,所以我不确定为什么它不能正确输出.同样,如果我将其更改为
资源.描述
那么什么也没得到输出.
Of course now the code runs but nothing is printed out (inside the <span>
tag). 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. Similarly if I just change it to
resource.Description
then nothing still gets output.
由于它在这种情况下效果很好,所以我想知道Razor的内联助手不会捕获外部变量吗?
Since it works well outside of this context, I wonder does Razor's inline helpers not capture the outer variables?
实际上HelperResult是Microsoft希望您不使用的东西,如文档所示:
Actually HelperResult is something Microsoft would rather you didn't use, as evidenced by documentation:
公共类HelperResult:名称空间中的IHtmlString System.Web.WebPages
public class HelperResult : IHtmlString in namespace System.Web.WebPages
摘要:此类型/成员支持.NET Framework基础结构 不能直接在您的代码中使用.
Summary: This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.
可能的解决方案是将描述函数包装在另一个帮助程序中,然后将该帮助程序作为
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)
在任何情况下,您的第一个想法都不可行,因为Func类型的参数实际上等于无参数函数,在这种情况下,您需要这样编写lambda表达式:
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:
myFunction( () => doSomething)
所以您的函数调用应该是:
So your function call would have been:
@node("title", () =>
@<text>
<span>"desc1"</span>
<span>"desc2"</span>
</text>)
由于这些助手的未来是有点可疑,因此,我会考虑改用 Partials (用于更大的块).
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.