使用 razor 在 Html.ActionLink 中使用 Lambda
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道 ActionLink,但似乎在这段代码中:
您正在声明一个 anonimus 类型,其中第一个字段名为“@title”,但是......第二个?!?这里我们看到一个带有三元运算符的表达式,但它的返回值没有分配给任何东西。
您必须在“item.Selected”之前添加标识符和赋值运算符:
如果 class="" 的值无效,您可以尝试将整个对象放入三元运算符中,如下所示:
但这可能不会工作:三元运算符将无法确定结果类型(两个匿名类型具有不同的签名)。
唯一的其他方法是首先使用“var”类型实例化正确的匿名对象,然后将其传递给方法。
无论如何,这里没有 lambda 表达式。您应该用“匿名类型”而不是“lambda”来标记您的答案。
I don't know ActionLink, but seems that in this piece of code:
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":
If a value of class="" is not valid, you could try to can put the entire object in ternary operator, like this:
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".
您是否尝试过:(
您缺少 HTML 属性名称)。
这是新的匿名类型语法而不是lambda,对于具有泛型类型的lambda,您必须在语句周围加上一组额外的括号。
Have you tried:
(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.