0

I have the following extension method:

public static MvcHtmlString GenerateBodyCellContentFor(
    this HtmlHelper helper,
    Goal goal,
    GoalProperty property)
    {
        if (property == GoalProperty.Name)
        {
            return LinkExtensions.ActionLink(
                helper,
                goal.Name,
                "Show",
                new
                {
                    goalId = goal.GoalId
                });
        }

        // rest of the code irrelevant
    }

This works as expected and generates the following link:

http://localhost:54913/Goal/Show?goalId=19013

Due to certain circumstances, I now need to explicitly specify the controller name. So I changed the method to the following:

public static MvcHtmlString GenerateBodyCellContentFor(
    this HtmlHelper helper,
    Goal goal,
    GoalProperty property)
    {
        if (property == GoalProperty.Name)
        {
            return LinkExtensions.ActionLink(
                helper,
                goal.Name,
                "Show",
                "Goal",                           // the only change in code
                new
                {
                    goalId = goal.GoalId
                });
        }

        // rest of the code irrelevant
    }

And the result, to my astonishment, is this:

http://localhost:54913/Goal/Show?Length=4

I tried removing and adding the controller parameter three times, because I could not believe that this actually happens. This is the ONLY change and it causes this behavior.

The length parameter is equal to controller name's length (just checked with a different string).

Do you have any idea what might be going on?

user622505
  • 743
  • 6
  • 23
  • question is similar to this one http://stackoverflow.com/questions/824279/why-does-html-actionlink-render-length-4 – sqladmin Aug 01 '14 at 15:33
  • Which [overload](http://msdn.microsoft.com/en-us/library/system.web.mvc.html.linkextensions.actionlink(v=vs.118).aspx) does that call match up with? Doesn't look like it fits.. – DGibbs Aug 01 '14 at 15:34
  • @DGibbs Indeed, no overload was fitting the parameters the way I wanted. – user622505 Aug 01 '14 at 15:38
  • @Lasooch That will probably explain where the `Length` is coming from. – DGibbs Aug 01 '14 at 15:39
  • @sqladmin that's helpful, thank you. I'll answer this question in case someone finds it instead of the one you linked. – user622505 Aug 01 '14 at 15:39
  • I encountered the same problem. Just as perplexed! But your solution also worked for me, so thanks! – Martin Vaughan Apr 27 '23 at 14:12

1 Answers1

2

As the answers to this question explain, the problem was caused by the fact that none of the overloads matched the way I needed them to. The simplest solution to this problem:

return LinkExtensions.ActionLink(
    helper,
    goal.Name,
    "Show",
    "Goal",
    new
    {
        goalId = goal.GoalId
    },
    null);
Community
  • 1
  • 1
user622505
  • 743
  • 6
  • 23