0

In an app there's a grid that has a column with the following code:

columns.Add(m => m.Ticket.Truck)
.Encoded(false)
.Sanitized(false)
.RenderValueAs(m =>
    @<div>
        @if (m.Ticket.Truck.ToLower() == "rusty")
        {
            <span style="color:red; font-size:14px; font-weight:bold;">@m.Ticket.Truck</span>
        }
        else
        {
            <span>@m.Ticket.Truck</span>
        }
    </div>).Titled("Truck");

In the dataset, most trucks have a number as their identifier, although there are some trucks with names (Rusty, for instance). Rather than hardcode in all the truck names, I'd like to modify the code so it checks @m.Ticket.Truck to see if it's a number, then only apply the tag styles if it's not a number. However, I don't know the correct syntax for doing this in Razor.

The posts I've found only show how to deal with this on the server side. Any guidance for how to do this in Razor would be greatly appreciated.

knot22
  • 2,648
  • 5
  • 31
  • 51

1 Answers1

0

Thanks to this post I figured it out. Also refactored the code.

        columns.Add(m => m.Ticket.Truck)
        .Encoded(false)
        .Sanitized(false)
        .RenderValueAs(m =>
    @<div>
    @{ 
        string style = "";
        int value;
        if (!int.TryParse(m.Ticket.Truck, out value))
        {
            // Truck is not an integer so set CSS
            style = "style=color:red;font-size:14px;font-weight:bold;";
        }
    }
         <span @style>@m.Ticket.Truck</span>
    </div>).Titled("Truck");

Note that there are purposely no spaces after the semi-colons in the style string. When spaces were present, Razor did not handle it correctly in the HTML.

knot22
  • 2,648
  • 5
  • 31
  • 51