ASP.NET Razor HTML - 根据值更改表行的背景颜色

der*_*rek 8 asp.net html5 razor

我正在使用ASP.NET c#使用razor视图引擎开发一个网站.我使用for循环来显示数据库中的行并将其显示在html表中.每行包含一个名为"requestStatus"的变量.请求状态为"已批准","已拒绝"或待处理.有没有办法可以根据requeststatus更改表行的bg颜色,例如,如果requeststatus是"pending",则将表行设置为黄色,如果请求状态为"approved",则将表行bgcolor设置为绿色?

任何帮助都会很棒!

我使用的代码显示表如下

 <fieldset>
            <legend>Your Leave Requests</legend>
            <table border="1" width="100%"> 



            <tr bgcolor="grey">
            <th>Description</th> 
            <th>Leave Type</th> 
            <th>Start Date</th> 
            <th>End Date</th> 
            <th>Total days leave requested</th> 
            <th>Request Status</th> 
            </tr>

           @foreach(var rows2 in rows1){


            <tr>

            <th>@rows2.description</th>
            <th>@rows2.leaveType</th> 
            <th>@rows2.startDate.ToString("dd-MMMM-yyyy")</th> 
            <th>@rows2.endDate.ToString("dd-MMMM-yyyy")</th> 
            <th>@rows2.totalDays</th> 
            <th>@rows2.requestStatus</th> 
            </tr>
              }  
            </table>

            </fieldset>
Run Code Online (Sandbox Code Playgroud)

Sky*_*s87 21

只需使用requestStatus作为类名,并将样式指定为适当的:

<style type="text/css">
    .grey {
        background-color:grey;
    }
    .approved {
        background-color:green;
    }
    .rejected {
        background-color:red;
    }
    .pending {
        background-color:lime;
    }
</style>

<fieldset>
    <legend>Your Leave Requests</legend>
    <table border="1" width="100%">
        <tr class="grey">
            <th>Description</th>
            <th>Leave Type</th>
            <th>Start Date</th>
            <th>End Date</th>
            <th>Total days leave requested</th>
            <th>Request Status</th>
        </tr>

        @foreach (var rows2 in rows1)
        {

            <tr class="@rows2.requestStatus">
                <td>@rows2.description</th>
                <td>@rows2.leaveType</th>
                <td>@rows2.startDate.ToString("dd-MMMM-yyyy")</th>
                <td>@rows2.endDate.ToString("dd-MMMM-yyyy")</th>
                <td>@rows2.totalDays</th>
                <td>@rows2.requestStatus</th>
            </tr>
        }
    </table>

</fieldset>
Run Code Online (Sandbox Code Playgroud)