4

I need to add a class to this link:

 @Html.ActionLink("Sign Out", "LogOff", "Account") 

But when I do this:

@Html.ActionLink("Sign Out", "LogOff", "Account",new{@class="btn blue"})  

The link points to the Home controller, not the Account controller thus throwing a 404.

/Home/LogOff?Length=7

What am I doing wrong?

Thanks

2 Answers 2

16

Try using the proper overload of the ActionLink helper (yeah there are gazillions of overloads):

@Html.ActionLink(
    "Sign Out",                  // linkText
    "LogOff",                    // actionName
    "Account",                   // controllerName
    null,                        // routeValues
    new { @class = "btn blue" }  // htmlAttributes
)

whereas you were using:

@Html.ActionLink(
    "Sign Out",                    // linkText
    "LogOff",                      // actionName
    "Account",                     // routeValues
    new { @class = "btn blue" }    // htmlAttributes
)

See why your code is not working?

Yeah, Microsoft did a hell of a mess with those overload and if you are not careful you get caught into the trap.

Solution: read MSDN or use Visual Studio Intellisense (F12 while your cursor is over the ActionLink helper).

For that reason I prefer to write it in a mode explicit manner using C# 4.0 named parameters:

@Html.ActionLink(
    linkText:       "Sign Out",
    actionName:     "LogOff",
    controllerName: "Account",
    routeValues:    null,
    htmlAttributes: new { @class = "btn blue" }
)
Sign up to request clarification or add additional context in comments.

Comments

1

When calling the ActionLink, there are several overloaded functions to call this. The one you want to use is Html.ActionLink("Link Text", "Action Name", Controller", "Route Values", HTML Attributes")

So something like this: @Html.ActionLink("Sign Out", "LogOff", "Account", null, new{@class="btn blue"})

1 Comment

Ah, Darin beat me to it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.