编译器不匹配剃须刀代码中的右括号(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
更多推荐
发布评论