模板 Razor 委托 - 交替行
我如何扩展 Phil Haack 在这里提到的列表模板剃刀委托示例,以便我可以提供备用行 css 类?
http://haacked.com/archive/2011/02/27 /templated-razor-delegates.aspx
我想做类似的事情:
@comics.List(
@< tr class="@odd">
< td>@item.Title< /td>
< td>@item.Publisher< /td>
)
编辑:我不需要 javascript 或 css 解决方案。我需要能够支持较旧的浏览器和可能禁用了 javascript 的浏览器。
How can I extend this List template razor delegate example that Phil Haack mentions here, so that I can provide an alternate row css class?
http://haacked.com/archive/2011/02/27/templated-razor-delegates.aspx
I would like to do something similar to this:
@comics.List(
@< tr class="@odd">
< td>@item.Title< /td>
< td>@item.Publisher< /td>
)
Edit: I don't need a javascript or css solution. I need to be able to support older browsers and browsers that may have javascript disabled.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您必须有一个服务器端解决方案,类似这样的事情就可以做到:
它应该导致:
(odd = !odd)
是一个布尔测试加上一个 NOT 操作切换标志。If you must have a server side solution, something like this will do it:
It should result in:
The
(odd = !odd)
is a boolean test plus a NOT operation that toggles the flag.我认为仅使用 CSS 也可以解决您所说的问题:
在您编辑之后更新我的答案
我不熟悉这个
Razor
模板机制,感谢分享,无论如何我阅读了它以更好地理解它。不幸的是,这似乎不可能。由于底层
HelperResult
需要一个名为@item
的参数,因此无法添加更多参数。 这篇文章清楚地解释了这个问题。我想可以使用这种技术编写一个特定的表格模板并达到您想要的结果,但恕我直言,
foreach
路线更简单。I assume using just CSS would also solve your stated problem:
Update of my answer after your Edit
I was not familiar with this
Razor
templating mechanism, thanks for sharing, anyway I read up on it to understand it better.Unfortunately it does not seem possible. As the underlying
HelperResult
expects a single parameter named@item
and more parameters cannot be added. This article explains the issue clearly.I guess it is possible write a specific table template using thistechnique and achieve your desired result but IMHO the
foreach
route is simpler.