使用 razor 在 Html.ActionLink 中使用 Lambda

发布于 2024-10-16 10:00:14 字数 271 浏览 2 评论 0原文

在 ActionLink 中使用 Lambda 的正确方法是什么?
我正在尝试这个:

 @Html.ActionLink(item.PageName, "ContentPage", new { id = item.PageName }, new { @title = item.ToolTip, item.Selected == 1 ? "class=selected" : "" })

但由于某种原因出现错误,我无法弄清楚它的正确语法?
谢谢

what is the correct way to use Lambda in an ActionLink?
I'm trying this:

 @Html.ActionLink(item.PageName, "ContentPage", new { id = item.PageName }, new { @title = item.ToolTip, item.Selected == 1 ? "class=selected" : "" })

but get an error for some reason,i can't figure out the correct syntax for it?
Thanks

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

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

发布评论

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

评论(2

尸血腥色 2024-10-23 10:00:14

我不知道 ActionLink,但似乎在这段代码中:

 new { @title = item.ToolTip, item.Selected == 1 ? "class=selected" : "" }

您正在声明一个 anonimus 类型,其中第一个字段名为“@title”,但是......第二个?!?这里我们看到一个带有三元运算符的表达式,但它的返回值没有分配给任何东西。
您必须在“item.Selected”之前添加标识符和赋值运算符:

 new { @title = item.ToolTip, class = item.Selected == 1 ? "selected" : "" }

如果 class="" 的值无效,您可以尝试将整个对象放入三元运算符中,如下所示:

 item.Selected == 1 ? new { @title = ... } : new { @title = ..., class = "selected" }

但这可能不会工作:三元运算符将无法确定结果类型(两个匿名类型具有不同的签名)。
唯一的其他方法是首先使用“var”类型实例化正确的匿名对象,然后将其传递给方法。
无论如何,这里没有 lambda 表达式。您应该用“匿名类型”而不是“lambda”来标记您的答案。

I don't know ActionLink, but seems that in this piece of code:

 new { @title = item.ToolTip, item.Selected == 1 ? "class=selected" : "" }

you are declaring an anonimus type, whith a first field named "@title", but.... the second?!? Here we see an expression with a ternary operator, but it's return value isn't assigned to anything.
You have to add an identifier and an assignement operator before "item.Selected":

 new { @title = item.ToolTip, class = item.Selected == 1 ? "selected" : "" }

If a value of class="" is not valid, you could try to can put the entire object in ternary operator, like this:

 item.Selected == 1 ? new { @title = ... } : new { @title = ..., class = "selected" }

But likely this will not work: the ternary operator will not be able to determine the result type (the two anonimous type have different signature).
The only other way is to instantiate first the right anonimous object, using the "var" type, and then pass it to the metod.
In any case there are no lambda expressions here. You should tag your answer with "anonimous types" instead of "lambda".

挽梦忆笙歌 2024-10-23 10:00:14

您是否尝试过:(

@Html.ActionLink(item.PageName,"ContentPage",new { id = item.PageName },new { @title = item.ToolTip, selected = (item.Selected == 1) ? "class=selected" : "" })

您缺少 HTML 属性名称)。

这是新的匿名类型语法而不是lambda,对于具有泛型类型的lambda,您必须在语句周围加上一组额外的括号。

Have you tried:

@Html.ActionLink(item.PageName,"ContentPage",new { id = item.PageName },new { @title = item.ToolTip, selected = (item.Selected == 1) ? "class=selected" : "" })

(you were missing a HTML property name).

This is new anonymous type syntax rather than lambas, for lambdas with generic types you have to wrap an extra set of brackets around the statement.

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