Adding logic to view the cause of the parse error?

I have a view that looks something like this:

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

      

Which works great. I wanted to add some logic around the part that creates the 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>
    }
}

      

But this throws a parsing error:

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: {

      

Can anyone understand what might be causing this? When I search for matching parentheses in Visual Studio everything seems to be legal.

+3


source to share


1 answer


Since you are not closing the element <table>

, Razor treats the ending }

as part of the HTML, so the block is @if {

not closed.

Try the following:

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

      



Or:

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

      

+1


source







All Articles