jgauffin
jgauffin

Reputation: 101150

How can you specify a css class name on ActionLinks in Razor?

The following code generates an error:

@Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" });

I tried to use @ since class is a keyword. How should I write it when using razor?

Edit

The problem was not really the at sign, but that I didn't use blocks together with my if:

@if (blabla)
    @Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" });

Works:

@if (blabla)
{
    @Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" });
}

Up voted both answers since they made me realize the problem.

Upvotes: 32

Views: 52385

Answers (2)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039110

Simply:

@Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" })

will work in ASP.NET MVC 3 RC2. Razor is intelligent.

Upvotes: 8

Aleksei Anufriev
Aleksei Anufriev

Reputation: 3236

Try to write something like:

@(Html.ActionLink("Title", "action", new { id=1 }, new { @class = "myCssClass" }));

There is a good post about Razor related to your problem: ScottGu Blog

Upvotes: 48

Related Questions