Razor 支持 lambda 表达式吗?

发布于 2024-10-16 21:19:01 字数 736 浏览 0 评论 0原文

Razor 视图引擎是否支持 lambda 表达式/匿名方法?

我在 Razor 中表达以下内容时遇到困难:

@Model.ToList().ForEach(i =>
    {
        if (i.DealerName != null) 
        {
            <text> 
                @i.DealerName
            </text>
        }
    }

注意: 我知道可以使用 @foreach 解决此问题 但我需要一个类似的第三方 MVC 控件解决方案。它使用这种机制来设置控件的内容。它适用于 MVC .ASPX 视图,但无法使其与 Razor 一起使用。


MVC .ASPX 等效项(我想要转换为 Razor 语法的代码):

<% Model.ToList().ForEach(i =>
       {
           if (i.DealerName != null)
           { 
           %> <%=i.DealerName%> <%
           };
       }); 
%>

这适用于 ASP.NET MVC3 附带的 Razor 引擎。

Are lambda expressions/anonymous methods supported in the Razor view engine?

I am having difficulty expressing the following in Razor:

@Model.ToList().ForEach(i =>
    {
        if (i.DealerName != null) 
        {
            <text> 
                @i.DealerName
            </text>
        }
    }

Note: I know can solve this with @foreach but I need a similar solution for a 3rd party MVC control. It using this mechanism for setting the content of the control. It works fine for MVC .ASPX views but cannot get it to work with Razor.

MVC .ASPX equivalent (the code I would like to convert to Razor syntax):

<% Model.ToList().ForEach(i =>
       {
           if (i.DealerName != null)
           { 
           %> <%=i.DealerName%> <%
           };
       }); 
%>

This is for the Razor engine that ships with ASP.NET MVC3.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

他夏了夏天 2024-10-23 21:19:01

您可以使用 Response.Write(i.DealerName); 代替 @i.DealerName 块,

结果是相同的,如下所示如果你把它放在 Razor 页面中 - 它将在渲染页面时执行。坦率地说 - 我很确定这就是它将被编译成的内容。

另外,由于 ForEach() 返回 void,因此您必须将其作为代码块放入页面中。
所以你的代码看起来像这样:

@{
    Model.ToList().ForEach(i =>
    {
        if (i.DealerName != null) 
        {
            Response.Write(i.DealerName);
        }
    });
}

UPD:如果你有更严格的格式,你可以诉诸这个漂亮的小技巧:
(不幸的是,这里的代码着色不会给这个片段任何信用,但如果你把它放到 Visual Studio 中,你肯定会明白我的意思。注意:这仅适用于 Razor 页面,不适用于代码文件:))

@{
    Model.ToList().ForEach(i =>
    {
        if (i.DealerName != null) 
        {
            Response.Write(((Func<dynamic, object>)(
                @<text>
                    <b>Hello Dealer named: @item.DealerName
                    Multiline support is <em>Beautiful!</em>
                </text>)).Invoke(i));
        }
    });
}

希望这使得感觉 :)

Instead of your <text>@i.DealerName</text> block you could use a Response.Write(i.DealerName);

The result is the same, as if you drop this in a Razor page - it will execute while rendering page.. And frankly - I'm pretty sure this is what it will be compiled into anyway.

Also, since ForEach() returns void, you'd have to drop it in the page as a code block.
So your code would look something like this:

@{
    Model.ToList().ForEach(i =>
    {
        if (i.DealerName != null) 
        {
            Response.Write(i.DealerName);
        }
    });
}

UPD: If you have more serious formatting, you can resort to this nice little trick:
(unfortunately the code colouring here will not give this snippet any credit, but you'll definitely see what I mean if you drop this in visual studio. Note: this will only work in Razor pages, not code files :) )

@{
    Model.ToList().ForEach(i =>
    {
        if (i.DealerName != null) 
        {
            Response.Write(((Func<dynamic, object>)(
                @<text>
                    <b>Hello Dealer named: @item.DealerName
                    Multiline support is <em>Beautiful!</em>
                </text>)).Invoke(i));
        }
    });
}

Hope that makes sense :)

阳光下的泡沫是彩色的 2024-10-23 21:19:01

或者,您可以创建一个 lambda 函数,并为 Razor 代码主体中的每个项目调用该函数(这个想法来自 Andy 在 this帖子):

@model IEnumerable<Dealer>

@{
    Func<Dealer, object> sayHi = 
        @<text>
             <b>Hello Dealer named: @(item.DealerName)</b>
             Multiline support is <em>Beautiful!</em>
         </text>;
}

<div>
    @foreach(var dealer in Model.ToList())
    {
        sayHi(dealer);
    }
</div>

Alternatively, you can create a lambda function, and call that for each item in the body of your Razor code (the idea came from Andy in this post):

@model IEnumerable<Dealer>

@{
    Func<Dealer, object> sayHi = 
        @<text>
             <b>Hello Dealer named: @(item.DealerName)</b>
             Multiline support is <em>Beautiful!</em>
         </text>;
}

<div>
    @foreach(var dealer in Model.ToList())
    {
        sayHi(dealer);
    }
</div>
寻找我们的幸福 2024-10-23 21:19:01

是的,他们受到支持。但是,Razor 有一些奇怪的转义规则,额外的大括号有时会导致它阻塞,包括扩展 lambda 表达式中的大括号。

您可以稍微简化 @Artioms 答案,以使用 where 和可选的 select 子句删除那些额外的大括号

@{
    Model.ToList().ForEach(i =>
    {
        if (i.DealerName != null) 
        {
            Response.Write(i.DealerName);
        }
    });
}

Yay

@{
    Model.Where(i=>i.DealerName != null).ToList().ForEach(i =>
    {
            Response.Write(i.DealerName);
    });
}

也可以成为

@{Model.Where(i=>i.DealerName != null).Select(i=>i.DealerName)
    .ToList().ForEach(Response.Write);}

函数样式!

Yes, they are supported. BUT, Razor has some weird escaping rules and extra braces will cause it to choke sometimes, including those in extended lambda expressions.

You can simplify the @Artioms answer a bit to remove those extra braces with a where and optionally a select clause

@{
    Model.ToList().ForEach(i =>
    {
        if (i.DealerName != null) 
        {
            Response.Write(i.DealerName);
        }
    });
}

becomes

@{
    Model.Where(i=>i.DealerName != null).ToList().ForEach(i =>
    {
            Response.Write(i.DealerName);
    });
}

Could also become

@{Model.Where(i=>i.DealerName != null).Select(i=>i.DealerName)
    .ToList().ForEach(Response.Write);}

Yay functional styles!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文