Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Style based on Enum value

I have an enum as follows:

 enum ProgressStatus{
   Approved,
   Unapproved,
   InProcess
 }

I have an Project object that use this class. Based on the enum values I need to show their values as Green, Yellow or Blue in while styling.

<ol class="breadcrumb">
      @if (Model.ProgressStatus == ProgressStatus.Approved)
      {
        <li style="color:red;"><label>Vaziyet Planı</label></li>
      }

</ol>

This is how i try to do it but it get cumbersome and clumsy.

What is the correct way to get this done?

like image 742
DarthVader Avatar asked Feb 13 '23 10:02

DarthVader


2 Answers

with your enum, you can use a short switch to set the color:

@{
    string color;
    switch(Model.ProgressStatus)
    { 
         case ProgressStatus.Approved : color = "green"; break; 
         case ProgressStatus.Unapproved : color = "yellow"; break; 
         //...
    }
 }

then you only need the one element

 <li style="color:@color;"><label>Vaziyet Planı</label></li>

This is just a different take on Stijn's answer really. I like his better.

like image 78
Jonesopolis Avatar answered Feb 15 '23 10:02

Jonesopolis


Put this in your view:

@{

    var labelColours = new Dictionary<ProgressStatus, string>()
    {
        { ProgressStatus.Approved, "green" },
        { ProgressStatus.Unapproved, "yellow" },
        { ProgressStatus.InProcess, "blue" }
    };
}

Then use this in your label:

<li style="color: @labelColours[Model.ProgressStatus];">
like image 41
user247702 Avatar answered Feb 15 '23 09:02

user247702