Razor variable inside ActionLink

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

string aboutLinkClass = "normalLink";

      

This may change depending on the context. Later in the view, I call Html.ActionLink and I need to use this variable, but the following does not give the desired result:

@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 create:

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

      

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

+3


source to share


1 answer


Try the following:

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

      



You are passing a string literal "@aboutLinkClass"

when you really want to pass your String

named object aboutLinkClass

.

+4


source







All Articles