8

According to this question, I am supposed to be able to write something like this:

@Html.ActionLink( "Delete", "Delete", "Message", new { data_id=id, @class="delete" } )

or as a happy T4MVC user can do:

@Html.ActionLink( "Delete", MVC.Message.Actions.Delete(), new { data_id=id, @class="delete" } )

And get the underscore in "data_id" replaced during rendering:

<a href="/message/delete" class="delete" data-id="42">Delete</a>

However, this seems not to work in the MVC 4 beta. Anyone else seeing this problem?

Is it an intentional change, and if so, what should I do instead?

UPDATE - HOW TO FIX (MANUALLY)

I've applied the following changes to the T4MVC.tt file, which fixes the problem in the generated code:

public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, object htmlAttributes, string protocol = null, string hostName = null, string fragment = null) {
    //was: return ActionLink(htmlHelper, linkText, result, new RouteValueDictionary(htmlAttributes), protocol, hostName, fragment);
    return htmlHelper.RouteLink(linkText, null, protocol, hostName, fragment, result.GetRouteValueDictionary(), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

UPDATE 2 - FIX AVAILABLE

David Ebbo was lightning fast at responding to the reported issue and has already merged the above fix into T4MVC.

UPDATE 3 - FIX THE FIX

Quite embarassingly, the original fix submitted did in fact not work, as it still called an invalid overload. I've now modified the code to do the same as MVC does internally (using their helper method), and notified David to have it included in T4MVC. Grab 2.6.70 from codeplex or update using NuGet when its released, probably shortly.

Community
  • 1
  • 1
Morten Mertner
  • 9,414
  • 4
  • 39
  • 56

1 Answers1

5

Try

@Html.ActionLink( "Delete", "Delete", "Message", null, new { data_id=id, @class="delete" } )

I think that becuase of all the overloads it is assuming your 4th parameter is the routeValues parameter.

StanK
  • 4,750
  • 2
  • 22
  • 46
  • Yes, I believe this is the correct answer. I have data-* attributes working in my MVC4 project, but have been tripped up by the ActionLink overloads many times – kaveman Mar 07 '12 at 04:37
  • That is a sensible suggestion, but I am actually using a T4MVC extension that takes an ActionResult, and this has no overload for route values (it's all contained in the ActionResult). I'm therefore confident that I am indeed passing the variable into the htmlAttributes parameter. Updated post to show how I really use it (just didnt want to confuse the picture with T4MVC). – Morten Mertner Mar 07 '12 at 04:38
  • Hey, who knew. You are right. T4MVC must be invoking the wrong overload in MVC4. Thanks! – Morten Mertner Mar 07 '12 at 04:43
  • 2
    i wish i had a dollar for every time i've come across this problem. There is definetely overloading issues in the MVC framework for actionlink/routelink. – RPM1984 Mar 07 '12 at 05:10