将html / view逻辑放在razor文件或viewmodel中

时间:2014-05-02 09:08:50

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

我有一个带整数列的表。

根据整数值,我想让单元格中的文本为红色或绿色。

这样的事情就像很多人一样:

List.cshtml:

string style = @DaysLeft <= 30 ? "background-color:Red" : "background-color:Green";
<td style="@style">
    ...
</td>

如果我将逻辑放在viewmodel中:

public ViewModel()
{
    BackgroundColorClass = DaysLeft > 100 ? "Background-color:red" : "Background-color:green";
}

和视图中的类分配:

<td  class="CenterOrientedColumns teststateField">
        @Html.LabelFor(m => m.DaysLeft, new{ @class = Model.BackgroundColorClass})
    </td>

为什么这个逻辑不属于viewmodel?

0 个答案:

没有答案
相关问题