添加逻辑以查看导致解析错误?

时间:2015-05-11 17:04:42

标签: asp.net asp.net-mvc razor

我有一个类似于以下内容的视图:

@foreach(var project in @Model.Projects.OrderBy(t=>t.ProjectTypeId))
{
   <h4>@project.ProjectType.Name</h4>
   <table class="table table-hover">
   ...
  </table>
}

哪个工作正常。我想在创建表的部分周围添加一些逻辑:

@foreach(var project in @Model.Projects.OrderBy(t=>t.ProjectTypeId))
{
   bool renderTable = true;
   if(renderTable)
   {    
      <h4>@project.ProjectType.Name</h4>
      <table class="table table-hover">
   }
   ...
    @if(renderTable)
    {
        </table>
    }
}

但是这导致了解析错误:

Parser Error

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

Parser Error Message: The foreach block is missing a closing "}" character.  Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.


Source Error: 


Line 15: 
Line 16: 
Line 17: @foreach(var project in @Model.Projects.OrderBy(t=>t.ProjectTypeId))
Line 18: {

任何人都能看到会导致这种情况的原因吗?当我在Visual Studio中搜索匹配的括号时,一切似乎都是合法的。

1 个答案:

答案 0 :(得分:1)

由于您未关闭<table>元素,因此Razor正在考虑将结尾}作为HTML的一部分,因此@if {块未关闭。

试试这个:

@:<table class="table table-hover">

或者:

if (renderTable)
{    
   <text>
       <h4>@project.ProjectType.Name</h4>
       <table class="table table-hover">
   </text>
}