How can I dynamically set @class in an ActionLink?
I want to do
@Html.ActionLink("Pricing", "Index", "Pricing", new { PageIndex = 2, @(ViewBag.PageIndex == 2 ? @class="" : @class="ActiveMenuItem" )}, null)
But the runtime blows up on my syntax.
Assuming you want "class" to be an HTML attribute and "PageIndex" to be an action parameter you can do this instead:
<a href="@Url.Action("Index", "Pricing")?PageIndex=2" class="@(ViewBag.PageIndex == 2 ? "ActiveMenuItem" : "")">Pricing</a>
MUSEFAN EDIT:
You can still use an ActionLink like this...
@Html.ActionLink("Pricing", "Index", "Pricing", new {PageIndex = 2}, new {@class = ViewBag.PageIndex == 2 ? "" : "ActiveMenuItem"})
@Html.ActionLink("Pricing", "Index", "Pricing",
new { PageIndex = 2, @class = (ViewBag.PageIndex == 2)? "" : "ActiveMenuItem" },
null)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With