MVC根据模型中的值更改行颜色

时间:2019-06-06 12:15:25

标签: asp.net-mvc colors row

我有那两栏,我想把那行放在哪里,旅行顾问=字母上带有绿色的Username1和字母上带有红色的username2。 我该怎么办?

<table class="table table-striped table-hover table-bordered tablesModel">
    <thead>
        <tr>

            <th data-field="@Html.DisplayNameFor(model => model.Travel_Consultant)" data-sortable="true">
                @Html.DisplayNameFor(model => model.Travel_Consultant)
            </th>

            @*scope1, scope2*@

            <th data-field="@Html.DisplayNameFor(model => model.Description)">
                @Html.DisplayNameFor(model => model.Description)
            </th>

        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td data-field="@Html.DisplayNameFor(model => model.Travel_Consultant)" data-sortable="true">
                    @Html.DisplayFor(modelItem => item.Travel_Consultant)
                </td>
                <td data-field="@Html.DisplayNameFor(model => model.Description)">
                    @Html.DisplayFor(modelItem => item.Description)
                </td>

            <td style="text-align:center;">
            <button onclick="createOrEdit(@item.ExtraJobsID)" type="button" class="btn-xs buttongreenpt">
            <i class="fa fa-edit fa-20px"></i>
            </button>
            </td>

            </tr>
        }
    </tbody>
</table>

2 个答案:

答案 0 :(得分:0)

在foreach循环中

if(consultant = Username1)
assign class Green to that row
.Green{color: green;}


else if(consultant = Username1)
assign class Red to that row
.Red{color: red;}

希望这可以解决您的问题。

答案 1 :(得分:0)

如果要在视图中应用代码,请在@foreach中尝试类似的操作:

          <tr>
            @if (model.Travel_Consultant == "Username1")
            {
            <td data-field="@Html.DisplayNameFor(model => model.Travel_Consultant)" 
            data-sortable="true" style="color: red">
                @Html.DisplayFor(modelItem => item.Travel_Consultant, new { @style = 
             "color:green;" })
            </td>

             }
             else if(model.Travel_Consultant == "username2")
             {
            <td data-field="@Html.DisplayNameFor(model => model.Description)">
                @Html.DisplayFor(modelItem => item.Description, new { @style = 
                "color:red;" })
            </td>

             }

        <td style="text-align:center;">
            <button onclick="createOrEdit(@item.ExtraJobsID)" type="button" class="btn-xs buttongreenpt">
                <i class="fa fa-edit fa-20px"></i>
            </button>
        </td>
    </tr>