Search code examples
asp.net-mvcasp.net-mvc-2actionlinkhtml.actionlink

ActionLink htmlAttributes with hyphens


This works

<a href="@Url.Action("edit", "markets", new { id = 1 })" 
            data-rel="dialog" data-transition="pop" data-icon="gear" class="ui-btn-right">Edit</a>

But this doesn't. Why?

@Html.ActionLink("Edit", "edit", "markets", new { id = 1 }, new {@class="ui-btn-right", data-icon="gear"})

It seems you can't pass something like data-icon="gear" into htmlAttributes?

Suggestions?


Solution

  • The problem is that your anonymous object property data-icon has an invalid name. C# properties cannot have dashes in their names. There are two ways you can get around that:

    Use an underscore instead of dash (MVC will automatically replace the underscore with a dash in the emitted HTML):

    @Html.ActionLink("Edit", "edit", "markets",
          new { id = 1 },
          new {@class="ui-btn-right", data_icon="gear"})
    

    Use the overload that takes in a dictionary:

    @Html.ActionLink("Edit", "edit", "markets",
          new { id = 1 },
          new Dictionary<string, object> { { "class", "ui-btn-right" }, { "data-icon", "gear" } });