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.
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.