tags:

views:

1311

answers:

4

I'm VERY confused as to why this code

Html.ActionLink("About", "About", "Home", new { hidefocus = "hidefocus" })

results in this link:

<a hidefocus="hidefocus" href="/Home/About?Length=4">About</a>

The hidefocus part is what I was aiming to achieve, but where does the "?Length=4" come from?

A: 

You are most likely missing a route in your Global.asax.cs file. Try changing the code to

Html.ActionLink("About", "About", "Home", new { id = "hidefocus" })

and see if that helps. If it does, and you specifically need the variable to be called hidefocus in your ActionMethod, try adding the following route above the default one:

routes.MapRoute("hidefocus", 
    "Home/About/{hidefocus}", 
    new {controller="Home", action="About", hidefocus = "hidefocus"});
Tomas Lycken
+4  A: 

The parameters to ActionLink are not correct, it's attempting to use the "Home" value as a route value, instead of the anonymous type.

I believe you just need to add new { } or null as the last parameter.

EDIT: Just re-read the post and realized you'll likely want to specify null as the second last parameter, not the last.

IMHO This is SOOOO freaking annoying. Happens to me 10 times a day.
Jabe
Tell me about it.
BTW They should remove one method as it's not possible to use it!
Marc Climent
+17  A: 

The Length=4 is coming from an attempt to serialize a string object. Your code is running this ActionLink method:

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

This takes a string object "Home" for routeValues, which the MVC plumbing searches for public properties turning them into route values. In the case of a string object, the only public property is Length, and since there will be no routes defined with a Length parameter it appends the property name and value as a query string parameter. You'll probably find if you run this from a page not on HomeController it will throw an error about a missing About action method. Try using the following:

Html.ActionLink("About", "About", new { controller = "Home" }, new { hidefocus = "hidefocus" })
roryf