4

I encountered a very weird behaviour in my project today. So I am working on a website which has an admin view an a common user view. The pages are stored in a folder named "Admin" and a folder named "User" under the folder "Views"

I started only setting up functionality for the Admin pages, so I never realized that my UserController didn't work. It always routed me to Admin/Somepage instead of User/Somepage.

After some testing I found the following problem:

If I use

@Html.ActionLink("Admin", "AdminHome", "Admin")
@Html.ActionLink("User", "UserHome", "User")

everything works just fine.

But as soon as I add a class to the link for example

@Html.ActionLink("User", "UserHome", "User", new { class= "someClass" })

it stops working. When I now click on the Link to the user homepage it routes to Admin/UserHome instead of User/UserHome and obviously can't find the page.

Why is that? Anyone ever experienced this?

I mean I can still wrap it in another div and add the class there. I just want to know if there is a reason behind this behaviour.

mypsi
  • 113
  • 1
  • 12
  • 2
    What happens if you use `new { @class= "someClass" }`? Notice that `class` is a reserved keyword so you need to escape it with a prefix `@` – Camilo Terevinto May 30 '18 at 11:57
  • 2
    Because it needs to be `@Html.ActionLink("User", "UserHome", "User", null, new { class= "someClass" })` –  May 30 '18 at 11:57
  • Then maybe a mix of the two above comments: `@Html.ActionLink("User", "UserHome", "User", null, new { @class= "someClass" })` – Rafalon May 30 '18 at 12:00

2 Answers2

3

Because as Stephen Muecke pointed out in his comment, the method you should be using is ActionLink(HtmlHelper, String, String, String, Object, Object) with the following signature:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    string controllerName,
    object routeValues,
    object htmlAttributes
)

@Html.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes)

where you currently are using ActionLink(HtmlHelper, String, String, Object, Object) with following signature:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper,
    string linkText,
    string actionName,
    object routeValues, // here you passed controllerName ("User") instead
    object htmlAttributes
)

@Html.ActionLink(linkText, actionName, routeValues, htmlAttributes)
Rafalon
  • 4,450
  • 2
  • 16
  • 30
0

You're using the wrong overload. By using this:

@Html.ActionLink("User", "UserHome", "User", new { class= "someClass" })

ASP.Net MVC thinks that the the fourth parameter is route values which is not the case.

To make it work as expected you need to use this overload that takes five parameters and set the attributes at the fifth position like below:

@Html.ActionLink("User", "UserHome", "User", null, new { class= "someClass" })

I set null at the fourth parameter because it seems like that your controller action doesn't need any route value.

CodeNotFound
  • 22,153
  • 10
  • 68
  • 69
  • Any reason why you linked the same link twice? – Rafalon May 30 '18 at 12:19
  • @Rafalon My bad. Fixed. Thanks. – CodeNotFound May 30 '18 at 12:21
  • You're welcome. However, please re-read the parts *fourth parameter* (first overload) and *five parameters* (second overload). It seems that you count `this` for the first overload, and you don't count it for the second. Or am I counting wrong? – Rafalon May 30 '18 at 12:27
  • Thank you! So bad that didn't realize my mistake. I thought cause the class Element is working and the AdminController works too the code should be fine. Missed that there is another parameter in between. – mypsi May 30 '18 at 12:57