剃刀语法错误

时间:2014-03-03 18:10:04

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

我想使用foreach创建一个表,列表中有n个元素,只有2列,然后添加一行。

我有这段代码,但我找不到正确的剃刀语法,唯一可行的方法就是删除带有if结尾的<tr>

我希望有人可以帮助我。

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

1 个答案:

答案 0 :(得分:2)

试试这个

@:将输出以下html内容

@让编译器将以下块视为C#。 Html将正确输出,因为它将识别为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>
         }
      }