user1404577
user1404577

Reputation:

How can i improve my code inside the Razor view to change the color of the text depending on the text content

i have the following code inside my razor view to change the color of the text according to the texty content:-

string status = ViewData[vlr.LabTestID.ToString()].ToString();

              if (status.ToUpper().StartsWith("Erro".ToUpper()))
                {
        <td style="color: #b30000">
       @status
        </td>
                }
             else   if (status.ToUpper().StartsWith("With".ToUpper()))
                {
        <td style="color: #6b9e52">
        @status
        </td>}
              else  if (status.ToUpper().StartsWith("Below".ToUpper()))
                {
        <td style="color: #b30000">
        @status
        </td>}
             else   if (status.ToUpper().StartsWith("Above".ToUpper()))
                {
        <td style="color: #b30000">
        @status
        </td>}
              else  if (status.ToUpper().StartsWith("Cannot".ToUpper()))
                {
        <td style="color: #5c87b2">
        @status
        </td>}
             }

But is there a way to perform the same functionality using a more reliable and more simpler approach than the above? BR

Upvotes: 3

Views: 1052

Answers (1)

David Spence
David Spence

Reputation: 8079

You could define a CSS style for each possibility and let that do the work.

Razor

<td class="@status.ToUpper()">
@status
</td>

CSS

.ERRO
{
    color: #6b9e52;
}

.WITH
{
    color: #b30000;
}

Upvotes: 2

Related Questions