Why does Html.ActionLink render "?Length=4"

Solution 1:

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" })

Solution 2:

The way I solved this is was adding a null to the fourth parameter before the anonymous declaration (new {}) so that it uses the following method overload: (linkText, actionName, controllerName, routeValues, htmlAttributes):

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

Solution 3:

You forgot to add the HTMLAttributes parm.

This will work without any changes:

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

Solution 4:

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.

Solution 5:

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

This will take the overload: string linkText, string actionName, string controllerName, Object routeValues, Object htmlAttributes