0

I have action link with login button

@Html.ActionLink("Login", "Login", "Account")

It works perfect redirecting user to Account controller Login action. But When I'm trying to add class to style this button:

@Html.ActionLink("Login", "Login", "Account", new { @class = "btn btn-primary btn-lg" }) 

It does get style and looks good, but it loses controller info and redirects user to Home/Login, instead of Account/Login

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
David
  • 4,332
  • 13
  • 54
  • 93

4 Answers4

2

You need to add a null parameter before your style, because it refers to routeValues:

@Html.ActionLink("Login", "Login", "Account", null, new { @class = "btn btn-primary btn-lg" }) 

Have a look at it's documentation Here.

Salah Akbari
  • 39,330
  • 10
  • 79
  • 109
1

The fourth parameter in your ActionLink helper refers to routeValues instead of htmlAttributes, which explains why you're redirected to another Login action.

You need to use ActionLink with 5 overloads like this:

@Html.ActionLink("Login", "Login", "Account", null, new { @class = "btn btn-primary btn-lg" })
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
1

There is object routeValues parameter after ControllerName and no such overload as ActionLink(link text, action name, controller name, htmlAttributes), so try this:

@Html.ActionLink("Login", "Login", "Account", null, new { @class = "btn btn-primary btn-lg" }) 
SᴇM
  • 7,024
  • 3
  • 24
  • 41
0

Instead of using this :

@Html.ActionLink("Login", "Login", "Account", new { @class = "btn btn-primary btn-lg" }) 

Use :

@Html.ActionLink("Login", "Login", "Account", null,new { @class = "btn btn-primary btn-lg" }) 

In 5 overloaded method you are setting htmlAttributes to the routeValues. That is why it doesn't behaves as you aspected.

Mohan Rajput
  • 634
  • 9
  • 21