根据条件结果在剃刀中分配类

时间:2013-01-11 00:21:37

标签: html razor

我目前有这段代码

@if(true) { <text> <td class="add"></td> </text> } else { <text> <td class="remove"></td> </text> }

我想改用这样的东西

<td> @if(true) { <text> @class="add" </text> } else { <text> @class="remove" </text> }</td>

我不确定这是正确的,还是正确的语法

1 个答案:

答案 0 :(得分:0)

@functions {
    object getAttributes(bool val)
    {
        if (val)
        {
            return new {@class="add"};
        }
        return new { @class="remove"};
    }
}

<td>
@Html.TextBoxFor(model => model.FirstName,@getAttributes(style))   
@getAttributes(style)
<td>