-2

I noticed something weird in my MVC project. I actually went back to the vanilla version to try it out and found out that the following (last list item being my only addition to the project, except for the action Test in the controller, only returning an empty instance of View) works well.

<ul class="nav navbar-nav">
  <li>@Html.ActionLink("Home", "Index", "Home")</li>
  <li>@Html.ActionLink("About", "About", "Home")</li>
  <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
  <li>@Html.ActionLink("Test", "Test", "Home")</li>
</ul>

However, when I added style to the action link as follows, I get an error. It works still but now VS remarks and when I check the remark, it asks me if I want to create a new controller Shared and an action Test.

<ul class="nav navbar-nav">
  <li>@Html.ActionLink("Home", "Index", "Home")</li>
  <li>@Html.ActionLink("About", "About", "Home")</li>
  <li>@Html.ActionLink("Contact", "Contact", "Home")</li>
  <li>@Html.ActionLink("Test", "Test", "Home", new { @style = "color: blue;" })</li>
</ul>

Why is it so? Is it a real problem at all?

tereško
  • 58,060
  • 25
  • 98
  • 150
Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • The overload you are using does not exits, route values comes before html attributes! – Transcendent Jan 01 '16 at 02:08
  • @Transcendent Well, technically speaking, the overload I'm using **does exist** but it's no the one I **intended to use**, hehe. Your comment follows the reply by David below, right? I'll take a look at it (later) in the morning. Is there anything I can improve with the question, BTW? I see a -1 and can't see why. – Konrad Viltersten Jan 01 '16 at 02:17
  • If you just took a look at the overload information that VS shows, there'd be no need to post the question. I once faced this problem and I just followed the overloads and I found that there's an object route value param right before the html attributes. It's seriously not a hard question to answer :) – Transcendent Jan 01 '16 at 02:19
  • @Transcendent I see where you're coming from. I'm to blame, I guess, for assuming that since it **was** running showing the **right** result on the screen, there was no issues with the syntax. It's just VS showing the name in red, so I got mislead. Good point, at any rate. – Konrad Viltersten Jan 01 '16 at 13:11

1 Answers1

4

There are a finite number of overloads to that method. Your "working" version is likely resolving to this one, which is explicitly referencing the "Home" controller as expected:

ActionLink("Test", "Test", "Home")

However, your "not working" version seems like it would reasonably resolve to this one, which "works" for the HTML attributes but changes what "Home" means in this case, treating it as a route value (which probably isn't parsed to anything useful and is ignored):

ActionLink("Test", "Test", "Home", new { @style = "color: blue;" })

Does it really generate the correct route in the client-side link? If so, I'd consider that a lucky coincidence but wouldn't rely on it.

You're probably looking for this overload, which would look like this:

ActionLink("Test", "Test", "Home", null, new { @style = "color: blue;" })
David
  • 208,112
  • 36
  • 198
  • 279
  • This one was tricky. You're right to your guess. However, even after I've read and understood your explanation (+1 for linkage) **twice**, I still didn't see the issue. Third time around I saw that the parameter changes from *string controllerName* to *object routeValues* on the **third** position. Confusing order of parameters (combined with me not reading all the details). And the intoxication due to 2016's entry might have contributed, haha. – Konrad Viltersten Jan 01 '16 at 13:20