ASP.NET Core 6 MVC / C#: change style color depending on ViewBag value

507 Views Asked by At

I am developing an ASP.NET Core 6 MVC application, I have a page with sorting and paging table.

The table columns have an Arrow Up and Arrow down to sort by column. When I do the sorting, in the controller I set a ViewBag.CurrentSort with the chosen ordering value.

When the page is rendering with that sort I want to change de color of that arrow...

If it arrow was selected, the color is Red, and if it not, the color is Black.

Here is an example of one row

<th scope="col">
    Name
    <span style="@ViewBag.CurrentSort = 'Name_ASC' ? 'color:Red':'color:Black'" 
          class="hqy-caret-up functionOver" 
          onclick="SearchOrder('Name_ASC')">
    </span>
    <span style="@ViewBag.CurrentSort = 'Name_DESC' ? 'color:Red':'color:Black'" 
          class="hqy-caret-down functionOver" 
          onclick="SearchOrder('Name_DESC')">
    </span>
</th>

I do not know how to ask an set the correct color in the Style

style="@ViewBag.CurrentSort = 'Name_ASC' ? 'color:Red':'color:Black'"

It does not work.

How can I implement it?

Thanks

1

There are 1 best solutions below

0
P. Magnusson On

You would need to use @() to define an inline code scope

<span style="@(ViewBag.CurrentSort == "Name_ASC" ? "color:Red" : "color:Black")"></span>