Eduardo Tovar
Eduardo Tovar

Reputation: 93

Error in razor syntax

I want to create a table using a foreach with n elements in a list, having only 2 columns, and then add a row.

I have this code but I can't find the correct razor syntax for it, the only way it works is removing the if with the ending of the <tr>.

I hope somebody can help me.

<table style="width: 100%">
    @{ 
        var vehiclesData = (IEnumerable<JMCapitalSeguros_Model.Entities.VehicleTable>)ViewData["Vehicles"]; 
        foreach (var vehicle in vehiclesData.Select((o, i) => new { Vehicle = o, Index = i }))
        {
            bool row = vehicle.Index % 2 == 0;
            if(row){ 
                <text>
                    <tr> 
                </text>   
            }

            <td width="50%">
                <label class="none">
                    @Html.RadioButtonFor(m => m.IdVehicleTable, vehicle.Vehicle.IdVehicleTable)@vehicle.Vehicle.Alias</label>
            </td>

            if(row)
            {
                <text>
                    </tr>
                </text>
            }

        }
    }
</table>

Upvotes: 2

Views: 92

Answers (1)

Onurbreak
Onurbreak

Reputation: 48

Try this

The @: will output the following html content

The @ let the compiler to consider the following block as C#. The Html will be correctly output since it will recognize as html

    <table style="width: 100%">
    @{ 
         var vehiclesData = (IEnumerable<JMCapitalSeguros_Model.Entities.VehicleTable>)ViewData["Vehicles"]; 
     }
    @foreach (var vehicle in vehiclesData.Select((o, i) => new { Vehicle = o, Index = i })) {
         bool row = vehicle.Index % 2 == 0;
         if(row){ 
             @:<tr> 
         }

         <td width="50%">
             <label class="none">@Html.RadioButtonFor(m => m.IdVehicleTable, vehicle.Vehicle.IdVehicleTable)@vehicle.Vehicle.Alias</label>
         </td>

         if(row){
             @:</tr>
         }
      }

Upvotes: 2

Related Questions