Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Razor variable inside ActionLink

I have variable for a CSS class value that is assigned to variable in a view:

string aboutLinkClass = "normalLink";

This can change based on context. Later in the view I call Html.ActionLink and I need to use that variable, but the following fails to produce the desired output:

@Html.ActionLink("About", "Index", "about", null, new {@class="@aboutLinkClass"})

It treats @aboutLinkClass as static text. so it produces:

<a class="@aboutLinkClass" href="/about">About</a>

Instead I want it to produce:

<a class="normalLink" href="/about">About</a>

What is the syntax I need to use to pass it correctly?

like image 926
Josh Avatar asked Aug 25 '26 06:08

Josh


1 Answers

Try this:

@Html.ActionLink("About", "Index", "about", null, new {@class = aboutLinkClass})

You're passing the string literal "@aboutLinkClass" when you actually want to pass your String object called aboutLinkClass.

like image 83
Ant P Avatar answered Aug 27 '26 23:08

Ant P