Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • René Andersen 238 posts 684 karma points
    Jun 13, 2012 @ 11:27
    René Andersen
    0

    Razor - Top navigation active link

    Hi

    See razor code below:

    <li class="@Library.If(isSelected, "active", "")">
      <a href="@page.Url">@page.Name</a>
     </li>

    Generates this:

     

    <li class="active"><a href="/kontakt.aspx">Kontakt</a></li>

    But i need it to generate this:

     

    <li><a href="about.html" class="active">About</a></li>

     

    Above it places "class="active" outside <li> and after href.

    Thank you in advance. :-)

     

  • Kasper Dyrvig 246 posts 379 karma points
    Jun 13, 2012 @ 11:51
    Kasper Dyrvig
    0

    Hi René

    Try moving the logic inside the a:

    <li>
     <a href="@page.Url" class="@Library.If(isSelected, "active", "")">@page.Name</a>
    </li>

    Hope it works

  • René Andersen 238 posts 684 karma points
    Jun 13, 2012 @ 12:41
    René Andersen
    0

    Hi Kasper

    Thank you!

    It works but i puts in a blank class="" on links that are not selected, does it matter?

  • Kasper Dyrvig 246 posts 379 karma points
    Jun 13, 2012 @ 13:23
    Kasper Dyrvig
    0

    You can solve it by moving the class="" inside the if. 

    <li>
     <ahref="@page.Url"@Library.If(isSelected, "class=\"active\"", "")>@page.Name</a>
    </li>

    Glad I could help. Please mark your post as solved so others can see this has an answer :-)

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies