Razor解析错误 - 在foreach循环中嵌套if和html

时间:2016-01-07 07:29:49

标签: asp.net-mvc razor

我试图在asp.net razor视图中执行以下代码 -

@foreach (var contact in ViewBag.ContactInfo.Rows)
{
    if (columnCount>4)
    {
        <div class="row-fluid">
    }

    <div class="span4">@ViewBag.SomeText</div>

    //This if block is treated as normal text.
    if (columnCount > 4)
    {
        </div>
        columnCount = 0;
    }

    columnCount++;
}

但它提供了解析错误。

任何帮助?

2 个答案:

答案 0 :(得分:1)

尝试以下内容。当你在一个中打开div标签并在其他方面关闭时,razor viewengine以某种方式无法解析它。检查here了解更多

      @foreach (var contact in ViewBag.ContactInfo.Rows)
        {
            if (columnCount>4)
            {
                <div class="row-fluid">
                <div class="span4">@ViewBag.SomeText</div>
                </div>
            }

            else
            {
                <div class="span4">@ViewBag.SomeText</div>
            }
            columnCount++;
        }

答案 1 :(得分:0)

@foreach (var contact in ViewBag.ContactInfo.Rows)
{
    if (columnCount>4)
    {
       @:<div class="row-fluid">
    }

   <div class="span4">@ViewBag.SomeText</div>

   //This if block is treated as normal text.
   if (columnCount > 4)
   {
      @:</div>
      columnCount = 0;
   }

   columnCount++;

}