2

I am building a menu with Razor and having a problem with it displaying properly. Here is the line that is giving me problems:

<li @topNavCurrentCount == @topNavCount ? "class=last" : @string.Empty><a href="@node.Url.Replace("~/","/").Replace(".aspx","")">@node.Title</a>

It renders the HTML like this:

 <li {1 == 7 ? "class=last" : ;}><a href="/Home">Home</a>

I know im close to getting it but I can't see it.

Todd
  • 1,780
  • 7
  • 32
  • 54

3 Answers3

2

try placing brackets around the if statement.

<li @(topNavCurrentCount == topNavCount ? "class=last" : string.Empty)>

@ next to a variable will print the value

Valamas
  • 24,169
  • 25
  • 107
  • 177
1

I think the following might work (placing brackets around the statement):

<li @( topNavCurrentCount == topNavCount ? "class=last" : string.Empty)><a href="@node.Url.Replace("~/","/").Replace(".aspx","")">@node.Title</a>
Jon Egerton
  • 40,401
  • 11
  • 97
  • 129
1

You need to enclose your logical expression with an @(). What's currently going on is that only your variable's values are being sent to the view.

See: Razor If/Else conditional operator syntax

Community
  • 1
  • 1
arviman
  • 5,087
  • 41
  • 48