1

I have this five ratings:

    if (model.CIIattainedrequired2021 > model.d4)
    {
        model.CIIrating2021 = "E";
    }
    if (model.CIIattainedrequired2021 < model.d4)
    {
        model.CIIrating2021 = "D";
    }
    if (model.CIIattainedrequired2021 < model.d3)
    {
        model.CIIrating2021 = "C";
    }
    if (model.CIIattainedrequired2021 < model.d2)
    {
        model.CIIrating2021 = "B";
    }
    if (model.CIIattainedrequired2021 < model.d1)
    {
        model.CIIrating2021 = "A";
    }

I display them in tables:

    <td class="text-center px-2 py-1 mx-0 text-xs">
    <p>@model.CIIrating2019</p>
</td><td class="text-center px-2 py-1 mx-0 text-xs">
    <p>@model.CIIrating2020</p>
</td><td class="text-center px-2 py-1 mx-0 text-xs">
    <p>@model.CIIrating2021</p>
</td><td class="text-center px-2 py-1 mx-0 text-xs">
    <p>@model.CIIrating2022</p>
</td><td class="text-center px-2 py-1 mx-0 text-xs">
    <p>@model.CIIrating2023</p>
</td><td class="text-center px-2 py-1 mx-0 text-xs">
    <p>@model.CIIrating2024</p>
</td><td class="text-center px-2 py-1 mx-0 text-xs">
    <p>@model.CIIrating2025</p>
</td><td class="text-center px-2 py-1 mx-0 text-xs">
    <p>@model.CIIrating2026</p>

And depending on the answer i would like to show the text in different colors as follow:

A-GREEN B-LIGHTGREEN C-YELLOW D-ORANGE E-RED

4
  • Your code example is weird, you have the same value in all your columns. Commented Jul 21, 2022 at 13:41
  • That was a mistake, i have edited,Thanks Commented Jul 21, 2022 at 14:27
  • 1
    @NikolasKypri Your model is probably not optimal (it would be more logical to have an object for each year). However, you could create a Dictionary<string, string> with the rating as key and the color as value. Commented Jul 21, 2022 at 14:37
  • I would recommend using a switch case instead of "if" condition Commented Jul 21, 2022 at 14:52

2 Answers 2

4

For the text, add this:

<p style="color:@TextColor(model.CIIrating2020);">@model.CIIrating2020</p>

Then in your code add something like this to return the correct color based on the text:

private string TextColor(string text) {
switch(text) 
{
  case "A":
    return "#00FF00"
    break;
  case "B":
    return "#90EE90"
    break;
  //etc....
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Could also do the same thing for a class yes?
0
  1. First thing you will need will be to add a class using a variable in your td element.
<td class="text-center px-2 py-1 mx-0 text-xs Add a class
        variable here like :@Color-code" >
  1. Then in the code section of the page/component you need the variable you are using as the class above and a method that switches classes based on your logic:
@code{
    private string Color-code{get;set;}

     -//method that identifies the color

     private void setColorClassMethod(){

         if(condition){
             //Assuming you want to assign blue class in the case
             Color-Code="blueClass";
        }
    }
}
  1. Finally in site.css file add colors to the classes you used above in variables like:
.blueClass{
 //if you want to change background color
 background-color:blue;
 
 //if you want to change text color
 color:blue;
}

You can also achieve the same results via in line css. But I suggested classes because you might need to add further styling later to the td.

2 Comments

A dictionary would be easier and more clear. With your solution, you need to create one variable for each CIIrating<year>. The colors don't need to change depending on year but on rating which does not need to be dynamically assigned.
Does this not work for many elements? It only seems to solve for one element which is alot of code for less value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.