pata
pata

Reputation: 989

Dynamically insert class in < li > with ViewBag

Using MVC 4.5 razor.

In my layout page, i have a menu with a list of < li > where i have links to different pages.

I need to set a class to a current's pages li. Atm im passing with the ViewBag.ActivePage the name of the li.

In pseudocode what i want to do is,

<li class="@{ViewBag.ActivePage == "Dashboard" ? "active" : "" }">...</li>
<li class="@{ViewBag.ActivePage == "Calendar" ? "active" : "" }">...</li>
<li class="@{ViewBag.ActivePage == "Serv" ? "active" : "" }">...</li>
<li class="@{ViewBag.ActivePage == "Prod" ? "active" : "" }">...</li>
etc, etc

But this aint working. How can i achieve this ??

Im setting ViewBag.ActivePage value on every page is loaded that use this layout.

Regards.

Upvotes: 2

Views: 468

Answers (1)

JC Lizard
JC Lizard

Reputation: 1056

here you go:

<ul>
    <li class="@(ViewBag.ActivePage == "Dashboard" ? "active" : "" )">...</li>
    <li class="@(ViewBag.ActivePage == "Calendar" ? "active" : "" )">...</li>
    <li class="@(ViewBag.ActivePage == "Serv" ? "active" : "" )">...</li>
    <li class="@(ViewBag.ActivePage == "Prod" ? "active" : "" )">...</li>
</ul>

Upvotes: 1

Related Questions