ActionLink htmlAttributes

asp.net Mvcasp.net Mvc-2Actionlinkhtml.actionlink

asp.net Mvc Problem Overview


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?

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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 - asp.net Mvc

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" .../>

Solution 3 - asp.net Mvc

@Html.ActionLink("Login", "Index", "hahgshg62jjshags",new { },new { rel = "nofollow", style = "color:white" })

Solution 4 - asp.net Mvc

@Html.ActionLink("display name", "action", "Contorller"
    new { id = 1 },Html Attribute=new {Attribute1="value"})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPavel HlobilView Question on Stackoverflow
Solution 1 - asp.net MvcmarcindView Answer on Stackoverflow
Solution 2 - asp.net MvchempView Answer on Stackoverflow
Solution 3 - asp.net MvcRaj KumarView Answer on Stackoverflow
Solution 4 - asp.net Mvcamirhossein fallahmaneshView Answer on Stackoverflow