Search code examples
c#razorrazor-2

razor syntax error while trying to divide (remainder) by 2 in clause statement?


How do you do this in ASP.NET Razor when trying to divide/remainder rowCounter by 2 ?? I get red underline syntax error saying "cannot implicity convert type 'long' to 'bool'

long rowCounter = 0;

foreach(var v in modelResult)
{
    @:<tr class='@(rowCounter % 2 ? "even" : "odd")'>

    rowCounter++;
}

Thanks...


Solution

  • The problem is that the result of rowCounter % 2 is a long, not a bool. You need to compare the result to something to see if rowCounter is actually odd or even. Try this:

    @:<tr class='@(rowCounter % 2 == 0 ? "even" : "odd")'>