Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysterious Length-Parameter in MVC Action

By mistake I took the wrong constructor in an ActionLink:

 @Html.ActionLink("Show Customer", "Load", "Customer", new {Model.Id });

The error is, that the last parameter is of type htmlAttributes and not of routeValues (as expected). So the right constructor would have been:

 @Html.ActionLink("Show Customer", "Load", "Customer", new {Model.Id }, null);

So I do not need to solve that problem... I just wonder, when I used the wrong constructor, my routeValue must have been interpreted as an htmlAttribute.

I was just surprised that it results in a length-Parameter. The generated code was:

/Customer/Load?Length=7

Just by curiosity: Where does the length=7 come from?

like image 661
Ole Albers Avatar asked Feb 15 '23 14:02

Ole Albers


2 Answers

This is overload of ActionLink, which is getting hit:

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

So your only route value is string, controller name, "Customer", which has public property Length, 8 symbols. And routeValues is picking with reflections all public properties of object, which was passed to it.

And worth to mention, that your link will receive html attribute Id='whatever_id_model_holds', since 4th parameter is mapped to htmlAttributes.

like image 179
Dmytro Avatar answered Feb 22 '23 22:02

Dmytro


TAKEN FROM https://stackoverflow.com/a/4360565/7720

The ActionLink override you are using matches to the (string linkText, string actionName, Object routeValues, Object htmlAttributes) override. So your "Customer" value is being passed to the routeValues parameter. The behavior of this function with respect to this parameter is to take all public properties on it and add it to the list of route values used to generate the link. Since a String only has one public property (Length) you end up with "length=7".

like image 27
Romias Avatar answered Feb 22 '23 22:02

Romias