的Razor视图引擎:复杂的循环和HTML [英] Razor View Engine: Complex looping and HTML

查看:399
本文介绍了的Razor视图引擎:复杂的循环和HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多在我当前的项目复杂的HTML报告,那里正进行大量的TR有条件的渲染和TD与rowspans和colspans的。

I have lots of complex HTML reports in my current project where there we perform lots of conditional rendering of TRs and TDs with rowspans and colspans.

这可能有时看起来像这样(这是非常简单):

It could sometimes look like this (this is extremely simplified):

<tr>
@foreach (var ourItem in ourList) {
   if (ourItem != ourList.First()) {
      <tr>                
   }
   <td></td>
   </tr>
}

然而,剃刀称:foreach循环缺少结束}字符。 (Visual Studio中)

However, Razor claims: "The foreach loop is missing a closing "}" character". (within Visual Studio)

我试着来包装&LT; TR&GT; &LT;文本&GT;&LT; /文本&GT; 这使得收盘}问题消失才发现这个运行时:没有匹配的开始标记是你的开始/结束标记适当的平衡。遇到结束标记TR

I've tried to wrap the <tr> in <text></text> which makes the closing } problem go away only to find this when run: "Encountered end tag "tr" with no matching start tag. Are your start/end tags properly balanced".

我怎么会做这种条件的渲染,而说服剃刀不要理会HTML可言,导致HTML是平衡的,当所有的循环完成。或者至少,那是当使用了ASP.NET视图引擎的情况。

How would I do this kind of conditional rendering while convincing Razor not to bother about the HTML at all, cause the HTML is balanced when all the looping is done. Or at least that was the case when the ASP.NET View Engine was used.

推荐答案

Visual Studio的智能感知和语法高亮是不是最好的之一,但在这种情况下,它会发出警告,如果条件不满足,你可能会得到无效的标记:和你不应该归咎于此。重要的是,你的项目运行良好,但你可能会考虑外部化这个逻辑转换为HTML佣工,因为如果你说的是真的,这是你在什么看法简化版本我甚​​至不想去想象怎么做您的实际code样子。恕我直言,有这么多的条件逻辑成为一个看法是滥用。你一定要考虑使用HTML佣工或如 MVCContrib电网控制。

Visual Studio Intellisense and syntax highlighting is not one of the best but in this case it warns you that if the condition is not satisfied you might get invalid markup: and you shouldn't blame it for this. The important thing is that your project runs fine but you might consider externalizing this logic into HTML helpers because if what you are saying is true that this is a simplified version of what you have in the views I don't even want to imagine how does your actual code look like. IMHO having so much conditional logic into a view is an abuse. You definitely should consider using HTML helpers or controls such as MVCContrib Grid.

更新:

您可以尝试下面的技巧:

You may try the following hack:

<tr>
@foreach (var ourItem in ourList) {
   if (ourItem != ourList.First()) {
      @:<tr>                
   }
   @:<td></td>
   @:</tr>
}

这篇关于的Razor视图引擎:复杂的循环和HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆