ActionLink htmlAttributes

149,851

Solution 1

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

Solution 2

Replace the desired hyphen with an underscore; it will automatically be rendered as a hyphen:

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

becomes:

<form action="markets/Edit/1" class="ui-btn-right" data-icon="gear" .../>
Share:
149,851
Pavel Hlobil
Author by

Pavel Hlobil

Hi

Updated on July 08, 2022

Comments

  • Pavel Hlobil
    Pavel Hlobil almost 2 years

    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>
    

    DOES NOT WORK - 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?

  • Dmitry Efimenko
    Dmitry Efimenko over 11 years
    The underscore does not seem to work with Ajax.ActionLink helpers
  • Michiel
    Michiel over 10 years
    The underscore trick sounds really strange, what if you want an underscore in your html attribute?
  • marcind
    marcind over 10 years
    @MichielReyers you could use the overload that takes in the Dictionary
  • niico
    niico about 7 years
    .net Core Tag Helpers destroy all these problems - hi from the future.