编译器不匹配剃须刀代码中的右括号(Compiler doesnt match the right brackets in razor code)

编程入门 行业动态 更新时间:2024-10-28 07:21:02
编译器不匹配剃须刀代码中的右括号(Compiler doesnt match the right brackets in razor code)

我试图在剃须刀代码中建立一个HTML表格,我在每一行放置两个项目。 我想到了底下的问题。 但是,编译器不会将正确的括号与另一个相匹配。 有什么方法可以解决这个问题吗?

for (int i = 0; i < items.Length; i++) { if (i == 0 || i % 2 == 0) { //this bracket matches... <tr> } //<td>plot item data here</td> if (i % 2 == 1 || i == items.Length - 1) { </tr> } //... with this bracket }

I'm trying to build an HTML table in razor code where I put two items on each row. I thought of the aproach underneath. However, the compiler won't match the correct brackets with one another. Is there some way to get around this?

for (int i = 0; i < items.Length; i++) { if (i == 0 || i % 2 == 0) { //this bracket matches... <tr> } //<td>plot item data here</td> if (i % 2 == 1 || i == items.Length - 1) { </tr> } //... with this bracket }

最满意答案

更新:我道歉,我误解了代码的目的。 如果我理解正确,你想从列表中获得2个项目并将它们放在一起,除非有奇数,最后只有一个项目?

如果是这样,我认为这应该起作用:

for (var i = 1; i <= list.Count + 1; i += 2) { <tr> if (i < list.Count) { <td>@(list[i - 1])</td> <td>@(list[i])</td> } else if (i == list.Count) { <td>@(list[i - 1])</td> } </tr> }

用小错误修复更新

Updated: I apologize, I misunderstood the purpose of the code. If I understand correctly, you want to get 2 items from a list and place them next to each other, except when there's an odd count, where the last would only have one item?

If so, this should work, I think:

for (var i = 1; i <= list.Count + 1; i += 2) { <tr> if (i < list.Count) { <td>@(list[i - 1])</td> <td>@(list[i])</td> } else if (i == list.Count) { <td>@(list[i - 1])</td> } </tr> }

updated with small bug-fix

更多推荐

本文发布于:2023-07-08 05:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1072504.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:剃须刀   编译器   括号   不匹配   代码

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!